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 gmail.com
35 #define EXIT_READ_FAILURE
37 #define FREE_ARG char*
40 #define CONDUCTOR_MINUS_ONE_V 5
41 #define CONDUCTOR_ZERO_V 10
42 #define CONDUCTOR_PLUS_ONE_V 15
44 #define CONDUCTOR_FLOATING 20
47 #define METAL_RIGHT 30
48 #define METAL_ABOVE 35
49 #define METAL_BELOW 40
50 #define METAL_BELOW_AND_LEFT 45
51 #define METAL_BELOW_AND_RIGHT 50
52 #define METAL_ABOVE_AND_LEFT 55
53 #define METAL_ABOVE_AND_RIGHT 60
55 #define DIELECTRIC 65 /* not for oddity */
56 #define ORDINARY_INTERIOR_POINT 70
58 #define TOP_LEFT_CORNER 75
59 #define BOTTOM_RIGHT_CORNER 80
60 #define TOP_RIGHT_CORNER 85
61 #define BOTTOM_LEFT_CORNER 90
62 #define ORDINARY_POINT_BOTTOM_EDGE 95
63 #define ORDINARY_POINT_TOP_EDGE 100
64 #define ORDINARY_POINT_LEFT_EDGE 105
65 #define ORDINARY_POINT_RIGHT_EDGE 110
66 #define DIFFERENT_DIELECTRIC_LOCALLY 115
68 #define DIFFERENT_DIELECTRIC_ABOVE_AND_RIGHT 120
69 #define DIFFERENT_DIELECTRIC_BELOW_AND_LEFT 125
70 #define DIFFERENT_DIELECTRIC_BELOW_AND_RIGHT 135
71 #define DIFFERENT_DIELECTRIC_VERTICALLY 140
72 #define DIFFERENT_DIELECTRIC_HORIZONTALLY 145
73 #define DIFFERENT_DIELECTRIC_BELOW 150
74 #define DIFFERENT_DIELECTRIC_LEFT 155
75 #define DIFFERENT_DIELECTRIC_RIGHT 160
76 #define DIFFERENT_DIELECTRIC_ABOVE_AND_LEFT 165
78 #define UNDEFINED_ODDITY 255
80 /* The following two determine what happens when a coupler is present
81 and the voltages have to be swapped from negative to positive in the
83 #define ITERATIONS 100
88 #define DONT_ZERO_ELEMENTS 0
89 #define ZERO_ELEMENTS_FIRST 1
101 #define VijB_TO_Vij 2
102 #define Vij_TO_VijB 3
104 #define MAXIMUM_PROCESSING_DEVICES 1024
105 #define MAX_DIFFERENT_PERMITTIVITIES 10000
113 #define Z_ODD_SINGLE_DIELECTRIC 1
114 #define Z_EVEN_SINGLE_DIELECTRIC 2
115 #define Z_ODD_MULTIPLE_DIELECTRIC 3
116 #define Z_EVEN_MULTIPLE_DIELECTRIC 4
120 /* The value of EPSILON_0 is taken from the UK National Physical
121 Laboratory's list of physical constants found on the web. */
123 #define EPSILON_0 8.854187817e-12
124 #define MU_0 M_PI*4e-7
125 #define MAX_THREADS 4
126 #define BORDER 5 /* The border to put around images. 1 is minimum, but 5 looks better */
129 #define VERY_LARGE 1e15
130 #define DEFAULT_BMP_SIZE 6
131 #define DEFAULT_COUPLER_BMP_SIZE 18
133 #define IMAGE_FIDDLE_FACTOR 2.0
134 #define ACCEPTABLE_ERROR 0.02
135 #define UNACCEPTABLE_ERROR 0.05
137 #define RECT_IN_RECT 1
138 #define CIRC_IN_CIRC 2
139 #define CIRC_IN_RECT 3
140 #define RECT_IN_CIRC 4
142 #define DEFAULT_GMIN 0.5
143 #define DEFAULT_GMAX 5.0
144 #define DEFAULT_GSTEP 0.5
146 #define DEFAULT_SMIN 0.5
147 #define DEFAULT_SMAX 5.0
148 #define DEFAULT_SSTEP 0.5
150 #define DEFAULT_WMIN 0.5
151 #define DEFAULT_WMAX 5.0
152 #define DEFAULT_WSTEP 0.5
157 #define MSG_TAG_WIDTH_HEIGHT 1
158 #define MSG_TAG_STRIP_PARAMS 2
159 #define MSG_TAG_NODE_TYPE 3
161 #define MSG_TAG_CONTROL 5
162 #define MSG_TAG_ITERATIONS 6
163 #define MSG_TAG_VIJ 7
164 #define MSG_TAG_VIJ_LBORDER 8
165 #define MSG_TAG_VIJ_RBORDER 9
166 #define MSG_TAG_ENERGY 10
167 #define MSG_TAG_DIELECTRICS 11
169 #define CONTROL_VALUE_RECEIVE_DATA 0
170 #define CONTROL_VALUE_SEND_DATA 1
171 #define CONTROL_VALUE_DO_ITERATIONS 2
172 #define CONTROL_VALUE_EXIT 3
173 #endif /* ENABLE_MPI */
176 extern char *my_optarg;
177 /* I needed to invent add my_ in front of the usual name for optind,
178 opterr, optopt etc, since they are defined by the system, and I'm using
179 a local package_version of getopts (get_options) so it exists with the PC too.
180 Most systems do not seem to mind if these names exist or not, but a
181 Power Machintosh powerpc running Darwin Kernel Package_version 5.3 gave this as
182 a warning. Hence I'll avoid the problem by adding my_ */
184 extern int my_optind, my_opterr, my_optopt;
186 struct transmission_line_properties{
187 double WW, HH, ww, ss, gg, hh, tt, Er1, Er2;
188 int W, H, w, s, g, h, t, bmp_size;
190 int verbose_level; /* 0, 1 or 2 */
191 double Codd, Ceven, C;
192 double velocity_odd, velocity_factor_odd, relative_permittivity_odd;
193 double velocity_even, velocity_factor_even, relative_permittivity_even;
194 double Lodd_vacuum, Leven_vacuum, L_vacuum;
195 double Codd_vacuum, Ceven_vacuum, C_vacuum, C_non_vacuum;
196 double Zo, Zo_vacuum, Zo_non_vacuum, Zodd, Zodd_vacuum, Zeven;
197 double Zeven_vacuum, Zdiff, Zdiff_vacuum, Zcomm, Zcomm_vacuum;
198 double Er, the_single_Er, Er_odd, Er_even;
199 double velocity, velocity_factor, relative_permittivity;
200 double r; /* rate multiplier */
201 double image_fiddle_factor;
202 double cutoff; /* How small the error in subsequent itterations must be */
203 int compute, display; /* none, ZO, ZODD, ZEVEN. ZEVEN_VAC, ZODD_VAC; */
204 int should_binary_data_be_written_tooQ;
205 int dielectrics_to_consider_just_now;
206 int non_vacuum_dielectric_found;
207 double found_this_dielectric;
208 int dielectrics_on_command_line;
209 int dielectrics_in_bitmap;
210 int avoid_use_of_fast_convergence_methodQ;
212 int write_binary_field_imagesQ;
213 int write_bitmap_field_imagesQ;
214 double float_values[10];
219 int non_metallic_pixels;
220 int tenth_of_estimated_iterations_needed;
223 #define NOT_IMPORTANT 0 /* The importance to attach to getting the */
224 #define IMPORTANT 1 /* best value of integers for the grid */
225 #define MOST_IMPORTANT 2
232 int white; /* Vacuum */
233 int other_colour; /* mix of red, green and blue */
239 double Ex_or_Ey_max, E_max, V_max, U_max, permittivity_max;
243 struct fit_doubles_to_integers{
246 double out[100], weight[100], good, acceptable;
247 double largest_errror;
256 #endif /* ENABLE_MPI */
258 /* Despite the fact the Bitmap_File_Head_Struct and Bitmap_Head_Struct
259 all have int's for the types of data, in practice these must be 2 and 4
260 byte varialbes. As such, they are written a character at a time, so the
261 data size is unimportant. This was neeed on the Cray, due to the fact
264 struct Bitmap_File_Head_Struct
266 unsigned char zzMagic[2]; /* 00 "BM" */
274 struct Bitmap_Head_Struct
276 int biWidth; /* 12 */
277 int biHeight; /* 16 */
278 int biPlanes; /* 1A */
279 int biBitCnt; /* 1C */
280 int biCompr; /* 1E */
281 int biSizeIm; /* 22 */
282 int biXPels; /* 26 */
283 int biYPels; /* 2A */
284 int biClrUsed; /* 2E */
285 int biClrImp; /* 32 */
290 #define M_PI 3.141592653589793238462643383279502884197169399375105820975
293 #define NUMBER_OF_DIELECTRICS_DEFINED 13
295 int main(int argc, char **argv);
296 void byteswap_doubles(double *a);
297 void read_bitmap_file_headers(char *filename, int *offset, size_t *size, int *width, int *height);
298 void help(char *filename);
299 double **dmatrix(long nrl, long nrh, long ncl, long nch);
300 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
301 void free_cmatrix(char **m, long nrl, long nrh, long ncl, long nch);
302 char *string(long nl,long nh);
303 void free_string(char *v, long nl, long nh);
304 void swap_bytes2(unsigned char *buffer, int offset, short *answer);
305 void swap_bytes4(unsigned char *buffer, int offset, int *answer);
306 void free_ustring(unsigned char *v, long nl, long nh);
307 void setup_arrays(struct transmission_line_properties *data);
308 double finite_difference_single_threaded();
309 double finite_difference_multi_threaded();
310 void *do_columns(void *thread_arg);
311 void usage_atlc(void);
312 void write_fields_for_two_conductor_lines(char *filename, struct transmission_line_properties data, size_t size);
313 void write_fields_for_directional_couplers(char *filename, struct transmission_line_properties data, size_t size, int odd_or_even);
314 char **cmatrix(long nrl, long nrh, long ncl, long nch);
315 unsigned char **ucmatrix(long nrl, long nrh, long ncl, long nch);
316 signed char **scmatrix(long nrl, long nrh, long ncl, long nch);
318 int *ivector(long nl, long nh);
319 void get_Er1_and_Er2_colours(int *colour_Er1, int *colour_Er2);
320 void convert_create_bmp_for_rect_in_rect_dimensions_to_integers(int bmp_size);
321 void convert_create_bmp_for_circ_in_circ_dimensions_to_integers(int bmp_size);
322 unsigned char *ustring(long nl,long nh);
323 void write_bitmap(FILE *image_data_fp, struct transmission_line_properties foo);
324 void usage_create_bmp_for_rect_in_rect(void);
325 void usage_create_bmp_for_rect_cen_in_rect(void);
326 void write_bitmap_out(unsigned char *image_data, FILE *image_data_fp,int image_size, int W, int H);
327 int align_bitmap_image(int W, int H, unsigned char *unaligned_image, unsigned char *byte_aligned_image);
328 void fill_create_bmp_for_rect_in_rect(char *image_vector, int colour_Er1, int colour_Er2);
329 void check_create_bmp_for_rect_in_rect_ints(void);
330 void exit_with_msg_and_exit_code(const char *error_text, int exitnumber);
331 void check_error(double user, int create_bmp_for_rect_in_rect, double gridsize, const char *name);
332 void check_parameters_for_create_bmp_for_circ_in_circ(void);
333 void fill_image_vector_with_data(unsigned char *image_vector, int colour_Er1, int colour_Er2, struct transmission_line_properties x);
334 void usage_create_bmp_for_circ_in_circ(void);
335 void convert_create_bmp_for_rect_in_circ_dimensions_to_integers(int accuracy_level);
336 void usage_create_bmp_for_rect_in_circ(void);
337 void convert_create_bmp_for_circ_in_rect_dimensions_to_integers(int accuracy_level);
338 void usage_create_bmp_for_circ_in_rect(void);
339 void check_for_shorts(void);
340 void usage_symmetrical_strip(void);
341 void fill_image_vector_for_thin_strip(int W,int H, int w, unsigned char *unaligned_image_vector);
342 double K_over_Kdash(double k);
343 double calculate_symmetrical_stripline_impedance(int H, int w);
345 void free_ucmatrix(unsigned char **m, long nrl, long nrh, long ncl, long nch);
346 void free_scmatrix(signed char **m, long nrl, long nrh, long ncl, long nch);
348 int print_data(FILE *fp, char *filename, double Er, double C, double L, double Zo, double
349 Zodd, double Zeven, int whichZ, double v, double vf);
350 void set_oddity_value(void);
351 double find_energy_per_metre(int i, int j);
352 FILE *get_file_pointer_with_right_filename(char *filename, const char *ext);
353 void find_maximum_values(struct max_values *maximum_values, int zero_elementsQ);
354 void calculate_colour_data(double x, double xmax, int w, int h, int offset, unsigned char *image_dat, int image_type,
355 unsigned char *red, unsigned char *green, unsigned char *blue, double image_fiddle_factor);
356 double find_Ex(int w, int h) ;
357 double find_Ey(int w, int h);
358 double find_E(int w, int h);
359 double voltage_ij(int i, int j);
360 void usage_readbin(void);
361 void ERR(const char *s, char c, char **argv);
362 int get_options(int argc, char **argv, const char *opts);
363 char *index2(const char *str, char c);
364 void swap_conductor_voltages();
365 void usage_create_bmp_for_stripline_coupler(void);
366 double calculate_integer_values(struct transmission_line_properties *optimise, int n, int accuarcy_level);
367 void calculate_Zodd_and_Zeven(double *Zodd, double *Zeven, double *Zo, double w, double H, double s, double er);
368 void usage_design_coupler(void);
369 void print_copyright(const char *s);
370 void give_examples_of_using_design_coupler(void);
371 void do_fd_calculation(struct transmission_line_properties *data, size_t size, FILE *where_to_print_fp, char *inputfile_filename);
372 void print_data_for_directional_couplers(struct transmission_line_properties data, FILE *where_to_print_fp, char *inputfile_name);
373 void print_data_for_two_conductor_lines(struct transmission_line_properties data, FILE *where_to_print_fp, char *inputfile_name);
374 void set_data_to_sensible_starting_values(struct transmission_line_properties *data);
375 void check_parameters_of_create_bmp_for_microstrip_coupler(struct transmission_line_properties pcb);
376 void convert_create_create_bmp_for_coupled_microstrip_dimensions_to_integers(struct transmission_line_properties *pcb);
377 void usage_create_bmp_for_microstrip_coupler(void);
378 void usage_find_optimal_dimensions_for_microstrip_coupler(void);
379 void check_parameters_of_create_bmp_for_circ_in_circ(void);
380 void check_parameters_of_create_bmp_for_circ_in_rect(void);
381 void check_parameters_of_create_bmp_for_rect_in_circ(void);
382 void check_parameters_of_create_bmp_for_rect_in_rect(void);
383 void usage_create_bmp_for_symmetrical_stripline(void);
384 void check_parameters_for_find_optimal_dimensions_for_microstrip_coupler (double h,double t,double Er1,double Er2,double ideal_Zodd,double ideal_Zeven);
385 double *dvector(long nl, long nh);
386 void *worker(void *thread_arg);
387 void free_ivector(int *v, long nl, long nh);
388 double check_convergence(double **grid1, double **grid2, int w, int h);
389 void error_check(char *s);
390 void free_dvector(double *v, long nl, long nh);
391 void usage_create_any_bitmap();
392 void update_voltage_array(int n, int starti, int endi, int i_index, int j_index, double **from, double **to);
393 void nrerror(char error_text[]);
394 void check_for_boundaries(void);