1 #include <stdio.h> 2 #include "nrutil.h" 3 #include "com_hack.h" 4 #include "nr_hack.h" 5 6 #ifndef TRUE 7 #define TRUE 1 8 #endif 9 10 #ifndef FALSE 11 #define FALSE 0 12 #endif 13 14 #define LIN 0 15 #define LOG 1 16 17 #define MAX_LINE 100 /* Maximum characters on one line of input file */ 18 19 #define DRIVEN_LENGTH 1 /* What to move - see randomis.c */ 20 #define DRIVEN_POSITION 2 21 #define REFLECTOR_LENGTH 4 22 #define DIRECTOR_LENGTH 8 23 #define DIRECTOR_POSITION 16 24 #define ALL_ELEMENT_LENGTHS_IDENTICAL 32 25 #define LINEAR_TAPER 64 26 #define RESONATE_DRIVEN 128 27 28 /* better criteria, see optimise and better.c */ 29 30 #define GAIN 1 31 #define FB 2 32 #define RESISTANCE 4 33 #define REACTANCE 8 34 #define VSWR 16 35 #define SIDE_LOBE_LEVEL 32 36 #define REASONABLE 32768 37 38 #define REASONABLE_G 39 #define REASONABLE_FB 27 40 #define REASONABLE_R 5 41 #define REASONABLE_X 5 42 #define REASONABLE_SWR 1.1 43 #define REASONABLE_SIDELOBE 23 44 45 #ifndef Z0 46 #define Z0 50 47 #endif 48 #define E_MAX 179.0 49 #define H_MAX 60.0 50 51 #define RRANGE_MIN -50.0 52 #define RRANGE_MAX 20.0 53 54 #define EULER 0.57721566 55 56 #define MAX_DRIVEN 6 /* There are 6 bits info on a driven ele */ 57 #define MAX_PARASITIC 4 /* But only 4 on a parasitic */ 58 59 #define X 1/* index into driven and parasitic arrays */ 60 #define Y 2 61 #define LENGTH 3 62 #define DIAMETER 4 63 #define VOLTAGE_R 5/* Volt and phase not on parasistic ele */ 64 #define VOLTAGE_I 6 65 #define REAL 0 66 #define IMAGINARY 1 67 #define HEADER_SIZE 100/* size of header in .out files in bytes */ 68 69 typedef struct element_data {double x,y,length;} fred; 70 71 typedef struct performance_data {double r,x,gain,fb,swr,sidelobe;} f4; 72 73 typedef struct flags {int aflg, bflg, cflg, dflg, eflg, errflg,fflg; 74 int gflg, hflg, iflg, jflg, kflg, lflg, mflg, nflg, oflg, pflg; 75 int qflg, rflg, sflg, tflg, uflg, vflg, wflg, xflg, yflg, zflg; 76 int Aflg, Bflg, Cflg, Dflg, Eflg, Fflg, Gflg, Hflg, Iflg, Jflg; 77 int Kflg, Lflg, Mflg, Nflg, Oflg, Pflg, Qflg, Rflg, Sflg, Tflg; 78 int Uflg, Vflg, Wflg, Xflg, Yflg, Zoflg;} f2; 79 80 typedef struct pattern { 81 double three_dB_E, three_dB_H; 82 double first_null_E, first_null_level_E; 83 double first_null_H, first_null_level_H; 84 double first_sidelobe_E, first_sidelobe_level_E; 85 double first_sidelobe_H, first_sidelobe_level_H; 86 }f3; 87 int main(int argc, char **argv); 88 int linear_current_optimisation_test(struct FCOMPLEX *cur, double *old_sd, int elements, int parasites, struct flags flag); 89 void display_currents( struct FCOMPLEX *currents, int elements); 90 void dynamic_changing_of_weights(int i, int div, struct performance_data *weight); 91 double Cin(double x); 92 double ci(double x); 93 double new_length(double oldl,double old_dia, double lambda, double new_dia); 94 void end_if_stop_exists(int *i, int iterations,int divisor); 95 struct performance_data subtract_structures(struct performance_data a, struct performance_data b); 96 void print_relavent_performance_data(FILE *fp, char *s, int i, struct flags flag, struct performance_data data,double fitness, int Z_Q, int fitness_Q); 97 /* char *s, int i, structure flags flag, \ 98 structure performance_data data); */ 99 void set_mean_structure(struct FCOMPLEX input_impedance,double E_fwd, double E_back,struct flags flag, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double f, double design_f, struct performance_data *mean); 100 void test_for_stop_file(void); 101 void set_performance_structures(struct performance_data *weight, struct performance_data *max, struct performance_data *best, struct performance_data *worst); 102 void optimising_for(struct flags flag); 103 double dB_down_from_peak(double x, double pin, struct element_data *coordinates, struct FCOMPLEX *current,int elements, double f,double design_f); 104 double find_max_sidelobe_fast(double gain, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double frequency,double design_f); 105 double find_max_sidelobe_slow(double gain, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double frequency,double design_f); 106 void copy_matrix(int length, int width, double **to, double **from); 107 char *get_data_filenames(int argc, char **argv, char *input); 108 char *string(long lower, long upper); 109 void free_string(char *string, long lower, long upper); 110 int get_number_of_elements(char *datafile, int *driven, int *parasitic); 111 void read_yagi_data(char *line, char *file, double *frequency, double *min_frequency, double*max_frequency, double *step_frequency, int dr, double **dr_data, int parasitic, double **para_data, double *angular_step); 112 void self_impedance(int i, double frequency, int driven, int parasitic, double **data, double **impedance); 113 void self2(double r, double l, double wavel, double *rin, double *xin); 114 void mutual_impedance(int i, int j, double frequency, int driven, int parasitic, double **d, double **p, double **impedance); 115 void fill_z_matrix(double frequency, int driven, int parasitic, double **d, double **p, double **impedance); 116 void z21(double lamda, double d, double mean_length, double *r21, double *x21); 117 double ci(double); 118 void fill_v_vector(int driven, int parasitic, double **d, double *v_vector); 119 void write_header_to_disk(FILE *ofp, int elements, int driven, 120 int parasitic, double min_frequency, double max_frequency, double frequency, 121 double step_frequency, double angular_step); 122 void write_vector(double *v, int elements, FILE *ofp); 123 int read_header(FILE *ifp, FILE *ofp, double *min_f, double *max_f, double *step_f, double *f, double *angular_step); 124 void z_input(struct FCOMPLEX v, struct FCOMPLEX i, struct FCOMPLEX *z); 125 void reflection_coefficient(struct FCOMPLEX z_input, double *mag, double *phase); 126 double calculate_vswr(double magnitude); 127 double calculate_power_input(double real_z, struct FCOMPLEX current); 128 void write_coordinates_of_elements_to_disk(FILE *ofp, int driven, 129 int parasitic, double **driven_data, double **parasitic_data); 130 void gain(double theta, double phi, double pin, double normallised_f, struct 131 element_data *cordinates,struct FCOMPLEX *current, int elements, double *E, double *H, double actual_frequency, double design_frequency); 132 struct FCOMPLEX E_to_complex_power(struct FCOMPLEX x); 133 struct FCOMPLEX **FCOMPLEXmatrix(long a, long b, long c, long d); 134 void free_FCOMPLEXmatrix(struct FCOMPLEX **m, long nrl, long nrh, long ncl, long nch); 135 void free_FCOMPLEXvector(struct FCOMPLEX *v, long nrl, long nrh); 136 void free_element_data_vector( struct element_data *v, long nl, long nh); 137 double change_max_percentage_changes(int i, int iterations, double orig_pc); 138 struct FCOMPLEX *FCOMPLEXvector(long a, long b); 139 struct element_data *element_data_vector(long low, long high); 140 void print_z_matrix(double f, int elements, double **z_matrix); 141 void write_gain_at_various_angles(FILE *gain_fp, double angular_step, double pin, double normalised_f, double f, struct element_data *coordinates, struct FCOMPLEX *current, int elements, double design_f); 142 143 void write_input_data_to_disk(FILE *fp, char *notes, double frequency, double min_f, double max_f, double step_f, int elements ,int driven,int parasitic, double angular_step,double **d, double **p, double scale_factor); 144 void randomise(int randomisation_method, double frequency, double percent, double **driven_data, double **parasitic_data, int driven, int parasitic); 145 void automatic_enhancement(struct flags flag, double frequency, double **driven_data, double **parasitic_data, int driven, int parasitic, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_Z, double *v, double **z, double **A, double *b, int *indx, struct element_data *coordinates); 146 147 /* int is_it_better(int criteria,struct performance_data new,struct performance_data best); */ 148 int is_it_better(int criteria, struct performance_data n, struct performance_data b); 149 double version(void); 150 void get_command_line_options(int argc, char **argv, struct flags *f); 151 void write_data_for_gnuplot(FILE *fp1,FILE *fp2,FILE *fp3,FILE *fp4,FILE *fp5,FILE *fp6, double f,double normalised_f,double input_impedance_r, double input_impedance_i, double vswr, double gain_E_plane,double fb_ratio); 152 void usage_first(char *str); 153 void usage_input(char *str); 154 void usage_yagi(char *str); 155 void usage_output(char *str); 156 void usage_optimise(char *str); 157 void write_file_for_gnuplot_to_load(FILE *fp, char *filename); 158 159 double performance(struct flags flag, struct performance_data data,struct performance_data weights, struct performance_data max, struct performance_data start); 160 int getoptions(int argc, char **argv, char *opts); 161 162 void copy_complex_data_to_real_matrix(int elements, double **from, double **to); 163 void show_all_first_parameters(char *exefile); 164 void show_all_optimise_parameters(char *exefile,struct flags flag); 165 double randreal(void); 166 int randint(void); 167 void solve_equations(double frequency, int driven, int parasitic, double **driven_data, double **parasitic_data, double *v, double **z, double *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_impedance, struct element_data *coordinates, double **A, double *b, int *indx); 168 void genetic_algorithm(char *output_filename, char *update_filename, struct flags flag, double frequency, double minf, double maxf, double stepf, double angular_step, int driven ,int parasitic, double **driven_data, double **parasitic_data, double *v, double **zz, double 169 *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX 170 *input_impedance, struct element_data *coordinates, double **A, double *b, int 171 *indx,struct performance_data *mean_performance); 172 173 double get_genetic_algorithm_fitness(struct flags flag, double frequency, int driven, int parasitic, double **driven_data, double **parasitic_data, double *v, double **z, double *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_impedance, struct element_data *coordinates, double **A, double *b, int *indx, struct performance_data *data2); 174 175 void check_flags(struct flags flag, int argc, int optind, char *exefilename); 176 void error_message(char *str); 177 double determine_maximum_gain(double f, double l); 178 double determine_maximum_gain2(int elements); 179 180 181 void do_since_better(int i, char *output_filename, char *update_filename, struct FCOMPLEX input_impedance, struct performance_data n,struct flags flag,char * notes,double frequency, double min_frequency,double max_frequency,double step_frequency,int elements, int driven,int parasitic,double angular_step,double **driven_data,double **parasitic_data,double scale_factor,double new_performance); 182 double gaussian(); 183 void sensitivity(double boom_sd, double length_sd, double **driven_data, double**parasitic_data, int driven, int parasites); 184 double log2(double x); 185 void mprove(double **a ,double **LU_of_a,int n, int *indx, double *b, double *x); 186 void seedRNG(void); 187 double error_3dB_E(double x); 188 double error_3dB_H(double x); 189 double zbrent(double (*func)(), double x1, double x2, double tol); 190 double Objective(char *gene); 191 int GA_Free(void); 192 int GA_Error(char *error_mesg); 193 void SetPrint(int a); 194 int Initialise(int popsise, int genesize); 195 int Selection(FILE *fd, int gene); 196 double ss2r(char *string, int pos, int len); 197 void write_gnuplot_header(FILE *fp, double f, char *filename, int step, int lin_or_log); 198 void self(double r, double length, double lambda, double *Rin, double *Xin); 199 void display_antenna_currents(struct FCOMPLEX *current, int elements); 200