[430] | 1 | #include "madx.h" |
---|
| 2 | |
---|
| 3 | /* extract SXF file from mad-X, or read SXF file into mad-X */ |
---|
| 4 | |
---|
| 5 | static double |
---|
| 6 | sequ_length, /* length of sequence */ |
---|
| 7 | sequ_start, |
---|
| 8 | sequ_end; |
---|
| 9 | |
---|
| 10 | // forward declarations |
---|
| 11 | |
---|
| 12 | static void sxf_fill_command(struct command*, int ntok, char** toks); |
---|
| 13 | |
---|
| 14 | // private functions |
---|
| 15 | |
---|
| 16 | static void put_line(FILE* out, char* s); |
---|
| 17 | |
---|
| 18 | static void |
---|
| 19 | r_indent(void) |
---|
| 20 | { |
---|
| 21 | if (b_level == 0) |
---|
| 22 | { |
---|
| 23 | printf("+=+=+= SXF_ex fatal - too many closing '}'\n"); |
---|
| 24 | exit(1); |
---|
| 25 | } |
---|
| 26 | indent = b_indent[--b_level]; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | #if 0 // not used |
---|
| 30 | static char* |
---|
| 31 | tag_spec(char* intype) |
---|
| 32 | { |
---|
| 33 | int j; |
---|
| 34 | |
---|
| 35 | for (j = 0; j < tag_cnt; j++) |
---|
| 36 | { |
---|
| 37 | if (strcmp(intype, tag_type[j]) == 0) return tag_code[j]; |
---|
| 38 | } |
---|
| 39 | return NULL; |
---|
| 40 | } |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | static int |
---|
| 44 | join_prefix(char* prefix, int ntok, char** toks) |
---|
| 45 | /* joins two tokens into one if the first is the "prefix" */ |
---|
| 46 | { |
---|
| 47 | int j, k; |
---|
| 48 | for (j = 0; j < ntok; j++) |
---|
| 49 | { |
---|
| 50 | if (strcmp(toks[j], prefix) == 0 && j+1 < ntok) |
---|
| 51 | { |
---|
| 52 | strcat(toks[j], toks[j+1]); |
---|
| 53 | for (k = j+1; k < ntok-1; k++) toks[k] = toks[k+1]; |
---|
| 54 | ntok--; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | return ntok; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | #if 0 // not used |
---|
| 61 | static int |
---|
| 62 | get_token_list /* returns no. of tokens */ |
---|
| 63 | (char* text, /* input: blank separated tokens */ |
---|
| 64 | char** tokens, /* output: token pointer list */ |
---|
| 65 | int max_tok) /* length of tokens */ |
---|
| 66 | { |
---|
| 67 | char* p; |
---|
| 68 | int count = 0; |
---|
| 69 | |
---|
| 70 | p = strtok(text," =;\n"); |
---|
| 71 | while (p != NULL && count < max_tok) |
---|
| 72 | { |
---|
| 73 | tokens[count++] = p; |
---|
| 74 | p = strtok(NULL," =;\n"); |
---|
| 75 | } |
---|
| 76 | return count; |
---|
| 77 | } |
---|
| 78 | #endif |
---|
| 79 | |
---|
| 80 | #if 0 // not used |
---|
| 81 | static int |
---|
| 82 | version_header(char* line) /* processes and checks header */ |
---|
| 83 | { |
---|
| 84 | int j = 0; |
---|
| 85 | char* header[] = {"//", "sxf", "version", "1.0"}; |
---|
| 86 | char* token_list[10]; |
---|
| 87 | if(get_token_list(line, token_list, 10) != 4) return 0; |
---|
| 88 | for (j = 0; j < 3; j++) |
---|
| 89 | { |
---|
| 90 | if (strcmp(header[j], stolower(token_list[j])) != 0) return 0; |
---|
| 91 | } |
---|
| 92 | return 1; |
---|
| 93 | } |
---|
| 94 | #endif |
---|
| 95 | |
---|
| 96 | static void |
---|
| 97 | reset_line(FILE* out) |
---|
| 98 | { |
---|
| 99 | if (all_blank(line) == 0) put_line(out,line); |
---|
| 100 | line[0] = '\0'; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | static void |
---|
| 104 | put_line(FILE* out, char* s) |
---|
| 105 | { |
---|
| 106 | char tline[2*MADX_LINE_MAX]; |
---|
| 107 | int i; |
---|
| 108 | if (s != line) reset_line(out); |
---|
| 109 | for (i = 0; i < indent; i++) tline[i] = ' '; |
---|
| 110 | strcpy(&tline[indent], s); |
---|
| 111 | fprintf(out, "%s\n",tline); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | static void |
---|
| 115 | s_indent(int i) |
---|
| 116 | { |
---|
| 117 | b_indent[b_level] = indent; |
---|
| 118 | indent += i; |
---|
| 119 | b_level++; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | static void |
---|
| 123 | accu_line(FILE* out, char* s) |
---|
| 124 | { |
---|
| 125 | if (strlen(line) + strlen(s) + indent > MADX_LINE_MAX) reset_line(out); |
---|
| 126 | strcpy(&line[strlen(line)], s); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | static void |
---|
| 130 | fill_dump(FILE* out, int flag, char* label, double* values, int count, int inc) |
---|
| 131 | { |
---|
| 132 | int j; |
---|
| 133 | |
---|
| 134 | if (flag == 0) sprintf(c_dum->c, " %s = ", label); |
---|
| 135 | else sprintf(c_dum->c, " %s = [", label); |
---|
| 136 | accu_line(out, c_dum->c); |
---|
| 137 | for (j = 0; j < count; j += inc) |
---|
| 138 | { |
---|
| 139 | sprintf(c_dum->c, " %.12g", values[j]); accu_line(out, c_dum->c); |
---|
| 140 | } |
---|
| 141 | if (flag != 0) |
---|
| 142 | { |
---|
| 143 | accu_line(out, "]"); reset_line(out); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | static int |
---|
| 148 | kl_trans(char* name, char* out_name, double* val, int* flag) |
---|
| 149 | { |
---|
| 150 | int j, length, rep; |
---|
| 151 | double corr; |
---|
| 152 | *flag = 0; |
---|
| 153 | |
---|
| 154 | if (strstr(name, "angle") != NULL){ |
---|
| 155 | *flag = 1; |
---|
| 156 | strcpy(out_name, "kl"); |
---|
| 157 | val[0] = node_value(name); |
---|
| 158 | return 1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | if (strstr(name, "kick") != NULL) |
---|
| 163 | { |
---|
| 164 | if (strchr(name, 'v') == NULL) |
---|
| 165 | { |
---|
| 166 | corr = current_node->chkick; |
---|
| 167 | strcpy(out_name, "kl"); |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
| 171 | corr = current_node->cvkick; |
---|
| 172 | strcpy(out_name, "kls"); |
---|
| 173 | } |
---|
| 174 | val[0] = node_value(name) + corr; |
---|
| 175 | if (val[0] != zero) return 1; |
---|
| 176 | else return 0; |
---|
| 177 | } |
---|
| 178 | else if (*name == 'k' && strcmp(name, "ks") != 0) |
---|
| 179 | { |
---|
| 180 | *flag = 1; |
---|
| 181 | if (strcmp(name, "knl") == 0 || strcmp(name, "ksl") == 0) |
---|
| 182 | { |
---|
| 183 | if (strchr(name, 's') == NULL) strcpy(out_name, "kl"); |
---|
| 184 | else strcpy(out_name, "kls"); |
---|
| 185 | length = element_vector(current_node->p_elem, name, val); |
---|
| 186 | if (length > 1 || val[0] != zero) return length; |
---|
| 187 | else return 0; |
---|
| 188 | } |
---|
| 189 | sscanf(&name[1], "%1d", &rep); |
---|
| 190 | for (j = 0; j < rep; j++) val[j] = zero; |
---|
| 191 | if (strchr(name, 's') == NULL) strcpy(out_name, "kl"); |
---|
| 192 | else strcpy(out_name, "kls"); |
---|
| 193 | if ((val[rep] = current_node->length * node_value(name)) != zero) |
---|
| 194 | return (rep+1); |
---|
| 195 | else return 0; |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | { |
---|
| 199 | strcpy(out_name, name); |
---|
| 200 | val[0] = node_value(name); |
---|
| 201 | if (strcmp(name, "e1") == 0 || strcmp(name, "e2") == 0) |
---|
| 202 | val[0] = command_par_value(name, current_node->p_elem->def); |
---|
| 203 | else val[0] = node_value(name); |
---|
| 204 | if (val[0] != zero) return 1; |
---|
| 205 | else return 0; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | static void |
---|
| 210 | write_elstart(FILE* out) |
---|
| 211 | { |
---|
| 212 | char name[NAME_L]; |
---|
| 213 | s_indent(add_indent[1]); |
---|
| 214 | if (current_node->occ_cnt > 1) /* add count to name, print warning */ |
---|
| 215 | { |
---|
| 216 | if (occnt_add++ == 0) |
---|
| 217 | printf("+=+=+= SXF_ex warning - making names unique\n\n"); |
---|
| 218 | strcpy(name, current_node->name); replace(name, ':', '.'); |
---|
| 219 | } |
---|
| 220 | else strcpy(name, current_node->p_elem->name); |
---|
| 221 | put_line(out, name); s_indent(add_indent[2]); |
---|
| 222 | sprintf(c_dum->c, "%s {", current_node->base_name); put_line(out, c_dum->c); |
---|
| 223 | s_indent(add_indent[3]); |
---|
| 224 | /* |
---|
| 225 | if (tag_flag == 1 && current_node->p_elem != current_node->p_elem->parent) |
---|
| 226 | { |
---|
| 227 | sprintf(c_dum->c, "tag = %s", current_node->p_elem->parent->name); |
---|
| 228 | put_line(out, c_dum->c); |
---|
| 229 | } |
---|
| 230 | else if (tag_flag == 2 && (pc = tag_spec(current_node->base_name)) != NULL) |
---|
| 231 | { |
---|
| 232 | sprintf(c_dum->c, "tag = %s", pc); |
---|
| 233 | put_line(out,c_dum->c); |
---|
| 234 | } |
---|
| 235 | */ |
---|
| 236 | |
---|
| 237 | sprintf(c_dum->c, "tag = %s", current_node->p_elem->name); |
---|
| 238 | put_line(out, c_dum->c); |
---|
| 239 | |
---|
| 240 | if (current_node->length > zero) |
---|
| 241 | { |
---|
| 242 | if (strstr(current_node->base_name, "bend") == NULL) |
---|
| 243 | sprintf(c_dum->c, "l = %.12g", current_node->length); |
---|
| 244 | else sprintf(c_dum->c, "arc = %.12g", current_node->length); |
---|
| 245 | put_line(out,c_dum->c); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /* |
---|
| 249 | sprintf(c_dum->c, "at = %.12g", current_node->position); |
---|
| 250 | nm printf("%s l = %.12g at = %.12g, prev at= %.12g\n", current_node->name, current_node->length, |
---|
| 251 | nm current_node->position, global_tmp_at); |
---|
| 252 | if(current_node->position < global_tmp_at) { |
---|
| 253 | printf("error: %s position (%.12g) < prev position (%.12g)\n", current_node->name, |
---|
| 254 | current_node->position, global_tmp_at); |
---|
| 255 | sprintf(c_dum->c, "at = %.12g", global_tmp_at); |
---|
| 256 | } |
---|
| 257 | else { |
---|
| 258 | sprintf(c_dum->c, "at = %.12g", current_node->position); |
---|
| 259 | global_tmp_at = current_node->position; |
---|
| 260 | } |
---|
| 261 | */ |
---|
| 262 | sprintf(c_dum->c, "at = %.12g", current_node->position); |
---|
| 263 | put_line(out,c_dum->c); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | static void |
---|
| 267 | write_field(FILE* out, struct double_array* fd) |
---|
| 268 | { |
---|
| 269 | int j; |
---|
| 270 | int k = -1; |
---|
| 271 | |
---|
| 272 | for (j = 0; j < fd->curr; j++) if (fd->a[j] != zero) k = j; |
---|
| 273 | if (++k > 0) |
---|
| 274 | { |
---|
| 275 | put_line(out, "body.dev = {"); s_indent(add_indent[4]); |
---|
| 276 | fill_dump(out, 1, "kl ", fd->a, k, 2); |
---|
| 277 | fill_dump(out, 1, "kls", &fd->a[1], k, 2); |
---|
| 278 | put_line(out, "}"); r_indent(); |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | static void |
---|
| 283 | write_align(FILE* out, struct double_array* al) |
---|
| 284 | { |
---|
| 285 | int j; |
---|
| 286 | int k = -1; |
---|
| 287 | |
---|
| 288 | for (j = 0; j < al->curr; j++) if (al->a[j] != zero) k = j; |
---|
| 289 | if (++k > 0) |
---|
| 290 | { |
---|
| 291 | put_line(out, "align.dev = {"); s_indent(add_indent[4]); |
---|
| 292 | fill_dump(out, 1, "al", al->a, k, 1); |
---|
| 293 | put_line(out, "}"); r_indent(); |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | static void |
---|
| 298 | write_elend(FILE* out) |
---|
| 299 | { |
---|
| 300 | put_line(out, "};"); |
---|
| 301 | r_indent(); r_indent(); r_indent(); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | static void |
---|
| 305 | write_body(FILE* out) |
---|
| 306 | { |
---|
| 307 | int i, flag = 0, nval, pos, set = 0; |
---|
| 308 | double val[100]; |
---|
| 309 | char out_name[NAME_L]; |
---|
| 310 | struct command* eldef; |
---|
| 311 | char npart[] = "npart"; |
---|
| 312 | eldef = current_node->p_elem->def; |
---|
| 313 | |
---|
| 314 | /* printf(" %s, , angle = %e \n", current_node->name, angle); */ |
---|
| 315 | for (i = 0; i < eldef->par_names->curr; i++) |
---|
| 316 | { |
---|
| 317 | /* nm printf(" i = %d, %s, value = %e\n", i, eldef->par_names->names[i], node_value(eldef->par_names->names[i])); */ |
---|
| 318 | double v = node_value(eldef->par_names->names[i]); |
---|
| 319 | if(v == 0.0 && strcmp(eldef->par_names->names[i], "knl") != 0) continue; |
---|
| 320 | /* nm printf(" i = %d, %s, angle = %e\n", i, eldef->par_names->names[i], angle); */ |
---|
| 321 | if (strcmp(eldef->par_names->names[i], "l") != 0 |
---|
| 322 | && (pos = name_list_pos(eldef->par_names->names[i], sxf_list)) > -1) |
---|
| 323 | { |
---|
| 324 | /* |
---|
| 325 | nm printf("kl_trans\n"); |
---|
| 326 | nm: ad hoc solution to avoid the SXF conceptual bug |
---|
| 327 | if((nval = angle_kl_trans(angle, sxf_list->names[pos], out_name, val, &flag, &angle_flag)) > 0) |
---|
| 328 | */ |
---|
| 329 | if ((nval = kl_trans(sxf_list->names[pos], out_name, val, &flag)) > 0) |
---|
| 330 | { |
---|
| 331 | if (set++ == 0) |
---|
| 332 | { |
---|
| 333 | put_line(out, "body = {"); s_indent(add_indent[4]); |
---|
| 334 | } |
---|
| 335 | fill_dump(out, flag, out_name, val, nval, 1); |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | if (strcmp(current_node->base_name, "beambeam") == 0) |
---|
| 342 | { |
---|
| 343 | if (set++ == 0) |
---|
| 344 | { |
---|
| 345 | put_line(out, "body = {"); s_indent(add_indent[4]); |
---|
| 346 | } |
---|
| 347 | val[0] = command_par_value(npart,current_beam); |
---|
| 348 | fill_dump(out, flag, npart, val, 1, 1); |
---|
| 349 | } |
---|
| 350 | if(set > 0) |
---|
| 351 | { |
---|
| 352 | put_line(out, "}"); r_indent(); |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | static void |
---|
| 357 | pro_elem_sxf(FILE* out) |
---|
| 358 | { |
---|
| 359 | if (strcmp(current_node->base_name, "drift") == 0) return; /* skip drifts */ |
---|
| 360 | write_elstart(out); |
---|
| 361 | write_body(out); sxf_elem_cnt++; |
---|
| 362 | if (current_node->p_fd_err && current_node->p_fd_err->curr > 0) |
---|
| 363 | { |
---|
| 364 | write_field(out, current_node->p_fd_err); sxf_field_cnt++; |
---|
| 365 | } |
---|
| 366 | if (current_node->p_al_err && current_node->p_al_err->curr > 0) |
---|
| 367 | { |
---|
| 368 | write_align(out, current_node->p_al_err); sxf_align_cnt++; |
---|
| 369 | } |
---|
| 370 | write_elend(out); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | static void |
---|
| 374 | sxf_rtag(void) |
---|
| 375 | { |
---|
| 376 | int j; |
---|
| 377 | char type[4][12] = {"kicker", "RBEND", "monitor", "vmonitor"}; |
---|
| 378 | char code[4][12] = {"kick", "rb", "mon", "vmon"}; |
---|
| 379 | |
---|
| 380 | tag_cnt = 4; |
---|
| 381 | for (j = 0; j < tag_cnt; j++) |
---|
| 382 | { |
---|
| 383 | strcpy(tag_type[j], type[j]); |
---|
| 384 | strcpy(tag_code[j], code[j]); |
---|
| 385 | stolower(tag_type[j]); stolower(tag_code[j]); |
---|
| 386 | } |
---|
| 387 | if (tag_cnt > 0) tag_flag = 2; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | static void |
---|
| 391 | sxf_write(struct command* cmd, FILE* out) |
---|
| 392 | /* writes the currently USEd sequence in SXF format to a file */ |
---|
| 393 | { |
---|
| 394 | (void)cmd; |
---|
| 395 | if (current_sequ == NULL || current_sequ->ex_start == NULL) |
---|
| 396 | { |
---|
| 397 | warning("SXFWRITE, but no active sequence:", "ignored"); |
---|
| 398 | return; |
---|
| 399 | } |
---|
| 400 | sxf_rtag(); |
---|
| 401 | put_line(out, "// SXF version 2.0"); |
---|
| 402 | sprintf(c_dum->c, "%s sequence", current_sequ->name); |
---|
| 403 | put_line(out, c_dum->c); |
---|
| 404 | s_indent(add_indent[0]); put_line(out, "{"); |
---|
| 405 | current_node = current_sequ->range_start; |
---|
| 406 | sequ_start = current_node->position; |
---|
| 407 | while (current_node) |
---|
| 408 | { |
---|
| 409 | if (strchr(current_node->name, '$') == NULL) pro_elem_sxf(out); |
---|
| 410 | if ((current_node = current_node->next) == current_sequ->range_end) break; |
---|
| 411 | } |
---|
| 412 | sequ_end = current_node->position; |
---|
| 413 | sequ_length = sequ_end - sequ_start; |
---|
| 414 | sprintf(c_dum->c, "endsequence at = %.12g", sequ_length); |
---|
| 415 | put_line(out, c_dum->c); |
---|
| 416 | indent = b_indent[--b_level]; |
---|
| 417 | put_line(out, "}"); |
---|
| 418 | put_line(out, "// SXF end"); |
---|
| 419 | printf("\nSXF_ex terminated - total number of elements: %d\n", sxf_elem_cnt); |
---|
| 420 | printf(" elements with alignment errors: %d\n", sxf_align_cnt); |
---|
| 421 | printf(" elements with field errors: %d\n", sxf_field_cnt); |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | static double |
---|
| 425 | find_value(char* name, int ntok, char** toks) |
---|
| 426 | /* returns value found in construct "name = value", or INVALID */ |
---|
| 427 | { |
---|
| 428 | double val = INVALID; |
---|
| 429 | int j; |
---|
| 430 | for (j = 0; j < ntok; j++) |
---|
| 431 | { |
---|
| 432 | if (strcmp(toks[j], name) == 0) |
---|
| 433 | { |
---|
| 434 | if (j+2 < ntok && *toks[j+1] == '=') |
---|
| 435 | { |
---|
| 436 | sscanf(toks[j+2], "%lf", &val); |
---|
| 437 | break; |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | return val; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | static int |
---|
| 445 | sxf_align_fill(int start, int end, int ntok, char** toks, double* vec) |
---|
| 446 | { |
---|
| 447 | int cnt = 0, j, rss, res; |
---|
| 448 | |
---|
| 449 | (void)end; |
---|
| 450 | if (strcmp(toks[start+1], "al") == 0) |
---|
| 451 | { |
---|
| 452 | get_bracket_t_range(toks, '[', ']', start+3, ntok, &rss, &res); |
---|
| 453 | if (rss == start+3) /* square bracket found */ |
---|
| 454 | { |
---|
| 455 | rss++; res--; |
---|
| 456 | for (j = rss; j <= res; j++) sscanf(toks[j], "%lf", &vec[cnt++]); |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | return cnt; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | static int |
---|
| 463 | sxf_field_fill(int start, int end, int ntok, char** toks, double* vec) |
---|
| 464 | { |
---|
| 465 | int cnt[2], i, j, k, rss, res; |
---|
| 466 | |
---|
| 467 | for (k = 0; k < 2; k++) |
---|
| 468 | { |
---|
| 469 | cnt[k] = 0; |
---|
| 470 | for (i = start+1; i < end; i++) |
---|
| 471 | { |
---|
| 472 | if ((k == 0 && strcmp(toks[i], "kl") == 0) || |
---|
| 473 | (k == 1 && strcmp(toks[i], "kls") == 0)) |
---|
| 474 | { |
---|
| 475 | get_bracket_t_range(toks, '[', ']', i+2, ntok, &rss, &res); |
---|
| 476 | if (rss == i+2) /* square bracket found */ |
---|
| 477 | { |
---|
| 478 | rss++; res--; cnt[k] = k; |
---|
| 479 | for (j = rss; j <= res; j++) |
---|
| 480 | { |
---|
| 481 | sscanf(toks[j], "%lf", &vec[cnt[k]]); |
---|
| 482 | cnt[k] += 2; |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | return (cnt[0] < cnt[1] ? cnt[1]-1 : cnt[0]-1); |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | static void |
---|
| 492 | sxf_body_fill(struct command* comm, int start, int end, int ntok, char** toks, double length) |
---|
| 493 | { |
---|
| 494 | struct name_list* nl = comm->par_names; |
---|
| 495 | struct command_parameter_list* pl = comm->par; |
---|
| 496 | int cnt, j, k, pos, rss, res, skew; |
---|
| 497 | double vec[20]; |
---|
| 498 | |
---|
| 499 | /* scan for magnet strengths first - special format */ |
---|
| 500 | for (skew = 0; skew < 2; skew++) |
---|
| 501 | { |
---|
| 502 | for (k = start+1; k < end; k++) |
---|
| 503 | { |
---|
| 504 | if (skew == 0 && strcmp("kl", toks[k]) == 0) break; |
---|
| 505 | else if (skew == 1 && strcmp("kls", toks[k]) == 0) break; |
---|
| 506 | } |
---|
| 507 | if (k < end) |
---|
| 508 | { |
---|
| 509 | get_bracket_t_range(toks, '[', ']', k, ntok, &rss, &res); |
---|
| 510 | if (rss == k + 2) /* square bracket found */ |
---|
| 511 | { |
---|
| 512 | rss++; res--; |
---|
| 513 | } |
---|
| 514 | else rss = res = k+2; |
---|
| 515 | cnt = 0; |
---|
| 516 | for (j = rss; j <= res; j++) sscanf(toks[j], "%lf", &vec[cnt++]); |
---|
| 517 | if (strstr(toks[1], "bend")) |
---|
| 518 | { |
---|
| 519 | if (length == zero) fatal_error("bend without length:", toks[0]); |
---|
| 520 | /*if (skew == 0) pos = name_list_pos("k0", nl); |
---|
| 521 | else pos = name_list_pos("k0s", nl);*/ |
---|
| 522 | /* nm printf("bend v[0] = %e; \n", vec[0]); */ |
---|
| 523 | pos = name_list_pos("angle", nl); |
---|
| 524 | pl->parameters[pos]->double_value = vec[0]; |
---|
| 525 | /* pl->parameters[pos]->double_value = vec[0] / length; */ |
---|
| 526 | } |
---|
| 527 | else if (strstr(toks[1], "quad")) |
---|
| 528 | { |
---|
| 529 | if (length == zero) fatal_error("quad without length:", toks[0]); |
---|
| 530 | if (skew == 0) pos = name_list_pos("k1", nl); |
---|
| 531 | else pos = name_list_pos("k1s", nl); |
---|
| 532 | pl->parameters[pos]->double_value = vec[1] / length; |
---|
| 533 | } |
---|
| 534 | else if (strstr(toks[1], "sext")) |
---|
| 535 | { |
---|
| 536 | if (length == zero) fatal_error("sextupole without length:", |
---|
| 537 | toks[0]); |
---|
| 538 | if (skew == 0) pos = name_list_pos("k2", nl); |
---|
| 539 | else pos = name_list_pos("k2s", nl); |
---|
| 540 | pl->parameters[pos]->double_value = vec[2] / length; |
---|
| 541 | } |
---|
| 542 | else if (strstr(toks[1], "oct")) |
---|
| 543 | { |
---|
| 544 | if (length == zero) fatal_error("octupole without length:", |
---|
| 545 | toks[0]); |
---|
| 546 | if (skew == 0) pos = name_list_pos("k3", nl); |
---|
| 547 | else pos = name_list_pos("k3s", nl); |
---|
| 548 | pl->parameters[pos]->double_value = vec[3] / length; |
---|
| 549 | } |
---|
| 550 | else if (strstr(toks[1], "kick")) |
---|
| 551 | { |
---|
| 552 | if (skew == 0) pos = name_list_pos("hkick", nl); |
---|
| 553 | else pos = name_list_pos("vkick", nl); |
---|
| 554 | pl->parameters[pos]->double_value = vec[0]; |
---|
| 555 | } |
---|
| 556 | else if (strstr(toks[1], "multi")) |
---|
| 557 | { |
---|
| 558 | if (skew == 0) pos = name_list_pos("knl", nl); |
---|
| 559 | else pos = name_list_pos("ksl", nl); |
---|
| 560 | while(pl->parameters[pos]->double_array->max < cnt) |
---|
| 561 | grow_double_array(pl->parameters[pos]->double_array); |
---|
| 562 | for (j = 0; j < cnt; j++) |
---|
| 563 | { |
---|
| 564 | pl->parameters[pos]->double_array->a[j] = vec[j]; |
---|
| 565 | } |
---|
| 566 | pl->parameters[pos]->double_array->curr = cnt; |
---|
| 567 | pl->parameters[pos]->expr_list |
---|
| 568 | = delete_expr_list(pl->parameters[pos]->expr_list); |
---|
| 569 | } |
---|
| 570 | } |
---|
| 571 | } |
---|
| 572 | /* now all the other parameters */ |
---|
| 573 | for (k = start+1; k < end; k++) |
---|
| 574 | { |
---|
| 575 | if (isalpha(*toks[k]) && strstr(toks[k], "kl") == NULL) |
---|
| 576 | { |
---|
| 577 | if ((pos = name_list_pos(toks[k], nl)) < 0) |
---|
| 578 | warning("unknown input parameter skipped: ", toks[k]); |
---|
| 579 | else if (k+2 < end && *toks[k+1] == '=' && *toks[k+2] != '[') |
---|
| 580 | sscanf(toks[k+2], "%lf", &pl->parameters[pos]->double_value); |
---|
| 581 | } |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | static void |
---|
| 586 | sxf_fill_command(struct command* comm, int ntok, char** toks) |
---|
| 587 | { |
---|
| 588 | struct name_list* nl = comm->par_names; |
---|
| 589 | struct command_parameter_list* pl = comm->par; |
---|
| 590 | int n, pos, rs, re; |
---|
| 591 | double length = zero; |
---|
| 592 | |
---|
| 593 | if ((pos = name_list_pos("l", nl)) > -1) |
---|
| 594 | { |
---|
| 595 | if (strstr(toks[1], "bend")) |
---|
| 596 | { |
---|
| 597 | if ((length = find_value("arc", ntok, toks)) == INVALID) |
---|
| 598 | length = find_value("l", ntok, toks); |
---|
| 599 | } |
---|
| 600 | else length = find_value("l", ntok, toks); |
---|
| 601 | if (length == INVALID) length = zero; |
---|
| 602 | pl->parameters[pos]->double_value = length; |
---|
| 603 | } |
---|
| 604 | for (n = 0; n < ntok; n++) if(strcmp("body", toks[n]) == 0) break; |
---|
| 605 | if (n < ntok) |
---|
| 606 | { |
---|
| 607 | get_bracket_t_range(toks, '{', '}', n, ntok, &rs, &re); |
---|
| 608 | if (rs < n) fatal_error("element body empty:", toks[0]); |
---|
| 609 | sxf_body_fill(comm, rs, re, ntok, toks, length); |
---|
| 610 | } |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | static int |
---|
| 614 | sxf_decin(char* p, int count) /* decode one SXF input item, store */ |
---|
| 615 | { |
---|
| 616 | int j, n, ntok, pos, rs, re; |
---|
| 617 | char** toks = tmp_p_array->p; |
---|
| 618 | struct command* clone; |
---|
| 619 | struct element* el; |
---|
| 620 | double at, vec[FIELD_MAX]; |
---|
| 621 | |
---|
| 622 | tmp_p_array->curr = 0; |
---|
| 623 | pre_split(p, aux_buff, 0); |
---|
| 624 | ntok = mysplit(aux_buff->c, tmp_p_array); |
---|
| 625 | ntok = join_prefix("-", ntok, toks); |
---|
| 626 | if (count == 0) |
---|
| 627 | { |
---|
| 628 | if (ntok < 2 || strcmp(toks[1], "sequence") != 0) return -1; |
---|
| 629 | else current_sequ = new_sequence(toks[0], 0); |
---|
| 630 | if (occ_list == NULL) |
---|
| 631 | occ_list = new_name_list("occ_list", 10000); /* for occurrence count */ |
---|
| 632 | else occ_list->curr = 0; |
---|
| 633 | current_sequ->cavities = new_el_list(100); |
---|
| 634 | current_sequ->crabcavities = new_el_list(100); |
---|
| 635 | pos = name_list_pos("marker", defined_commands->list); |
---|
| 636 | clone = clone_command(defined_commands->commands[pos]); |
---|
| 637 | sprintf(c_dum->c, "%s$start", current_sequ->name); |
---|
| 638 | el = make_element(c_dum->c, "marker", clone, 0); |
---|
| 639 | make_elem_node(el, 1); |
---|
| 640 | current_sequ->start = current_node; |
---|
| 641 | for (j = 3; j < ntok; j++) /* push first element down */ |
---|
| 642 | { |
---|
| 643 | toks[j-3] = toks[j]; |
---|
| 644 | } |
---|
| 645 | ntok -= 3; |
---|
| 646 | } |
---|
| 647 | else if (strcmp(toks[0], "endsequence") == 0) |
---|
| 648 | { |
---|
| 649 | current_sequ->length = find_value("at", ntok, toks); |
---|
| 650 | pos = name_list_pos("marker", defined_commands->list); |
---|
| 651 | clone = clone_command(defined_commands->commands[pos]); |
---|
| 652 | sprintf(c_dum->c, "%s$end", current_sequ->name); |
---|
| 653 | el = make_element(c_dum->c, "marker", clone, 0); |
---|
| 654 | make_elem_node(el, 1); |
---|
| 655 | current_node->at_value = current_sequ->length; |
---|
| 656 | current_sequ->end = current_node; |
---|
| 657 | current_sequ->start->previous = current_sequ->end; |
---|
| 658 | current_sequ->end->next = current_sequ->start; |
---|
| 659 | return 1; |
---|
| 660 | } |
---|
| 661 | if ((pos = name_list_pos(toks[1], defined_commands->list)) < 0) |
---|
| 662 | fatal_error("element type not found:", toks[1]); |
---|
| 663 | clone = clone_command(defined_commands->commands[pos]); |
---|
| 664 | sxf_fill_command(clone, ntok, toks); |
---|
| 665 | el = make_element(toks[0], toks[1], clone, sequ_is_on+1); |
---|
| 666 | if (strcmp(el->base_type->name, "rfcavity") == 0 && |
---|
| 667 | find_element(el->name, current_sequ->cavities) == NULL) |
---|
| 668 | add_to_el_list(&el, 0, current_sequ->cavities, 0); |
---|
| 669 | if (strcmp(el->base_type->name, "crabcavity") == 0 && |
---|
| 670 | find_element(el->name, current_sequ->crabcavities) == NULL) |
---|
| 671 | add_to_el_list(&el, 0, current_sequ->crabcavities, 0); |
---|
| 672 | add_to_name_list(el->name, 1, occ_list); |
---|
| 673 | make_elem_node(el, 1); |
---|
| 674 | sxf_suml += el->length / 2; |
---|
| 675 | if ((at = find_value("at", ntok, toks)) == INVALID) at = sxf_suml; |
---|
| 676 | else sxf_suml = at; |
---|
| 677 | sxf_suml += el->length / 2; |
---|
| 678 | current_node->at_value = at; |
---|
| 679 | for (n = 0; n < ntok; n++) if(strcmp("align.dev", toks[n]) == 0) break; |
---|
| 680 | if (n < ntok) |
---|
| 681 | { |
---|
| 682 | get_bracket_t_range(toks, '{', '}', n, ntok, &rs, &re); |
---|
| 683 | if (rs < n) fatal_error("alignment errors empty:", toks[0]); |
---|
| 684 | n = sxf_align_fill(rs, re, ntok, toks, vec); |
---|
| 685 | current_node->p_al_err = new_double_array(n); |
---|
| 686 | current_node->p_al_err->curr = n; |
---|
| 687 | for (j = 0; j < n; j++) current_node->p_al_err->a[j] = vec[j]; |
---|
| 688 | } |
---|
| 689 | for (n = 0; n < ntok; n++) if(strcmp("body.dev", toks[n]) == 0) break; |
---|
| 690 | if (n < ntok) |
---|
| 691 | { |
---|
| 692 | get_bracket_t_range(toks, '{', '}', n, ntok, &rs, &re); |
---|
| 693 | if (rs < n) fatal_error("field errors empty:", toks[0]); |
---|
| 694 | for (j = 0; j < FIELD_MAX; j++) vec[j] = zero; |
---|
| 695 | n = sxf_field_fill(rs, re, ntok, toks, vec); |
---|
| 696 | current_node->p_fd_err = new_double_array(n); |
---|
| 697 | current_node->p_fd_err->curr = n; |
---|
| 698 | for (j = 0; j < n; j++) current_node->p_fd_err->a[j] = vec[j]; |
---|
| 699 | } |
---|
| 700 | return 0; |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | static void |
---|
| 704 | sxf_read(struct command* cmd) |
---|
| 705 | /* reads an expanded sequence including errors from an SXF file */ |
---|
| 706 | { |
---|
| 707 | struct sequence* keep_sequ = current_sequ; |
---|
| 708 | int echo, err, izero = 0, count = 0; // n, not used |
---|
| 709 | FILE* in_file = in->input_files[in->curr]; |
---|
| 710 | char *p, *pp; |
---|
| 711 | |
---|
| 712 | (void)cmd; |
---|
| 713 | sxf_suml = zero; |
---|
| 714 | if (fgets(aux_buff->c, aux_buff->max, in_file) == NULL) |
---|
| 715 | { |
---|
| 716 | warning("SXF input file empty,"," ignored"); |
---|
| 717 | return; |
---|
| 718 | } |
---|
| 719 | /* |
---|
| 720 | if ((rcode = version_header(aux_buff->c)) == 0) |
---|
| 721 | { |
---|
| 722 | warning("SXF header missing or wrong,"," ignored"); |
---|
| 723 | return; |
---|
| 724 | } |
---|
| 725 | */ |
---|
| 726 | sequ_is_on = 1; |
---|
| 727 | echo = get_option("echo"); |
---|
| 728 | set_option("echo", &izero); |
---|
| 729 | get_stmt(in_file, 1); // n = not used |
---|
| 730 | replace(in->buffers[in->curr]->c_a->c, ',', ' '); |
---|
| 731 | replace(in->buffers[in->curr]->c_a->c, '\n', ' '); |
---|
| 732 | p = strtok(in->buffers[in->curr]->c_a->c, ";"); |
---|
| 733 | while(p) |
---|
| 734 | { |
---|
| 735 | pp = &p[strlen(p)+1]; |
---|
| 736 | if ((err = sxf_decin(p, count++)) == -1) |
---|
| 737 | { |
---|
| 738 | warning("No sequence name found, ", "ignored"); |
---|
| 739 | goto term; |
---|
| 740 | } |
---|
| 741 | else if (err == 1) break; |
---|
| 742 | p = strtok(pp, ";"); |
---|
| 743 | } |
---|
| 744 | if (current_sequ->length == zero) |
---|
| 745 | { |
---|
| 746 | warning("No endsequence with length found, ", "ignored"); |
---|
| 747 | current_sequ = keep_sequ; |
---|
| 748 | goto term; |
---|
| 749 | } |
---|
| 750 | printf("SXF -- sequence %s: declared length = %e; element l_sum = %e\n", |
---|
| 751 | current_sequ->name, current_sequ->length, sxf_suml); |
---|
| 752 | add_to_sequ_list(current_sequ, sequences); |
---|
| 753 | if (attach_beam(current_sequ) == 0) |
---|
| 754 | fatal_error("USE - sequence without beam:", current_sequ->name); |
---|
| 755 | current_sequ->beam = current_beam; |
---|
| 756 | current_range = tmpbuff("#s/#e"); |
---|
| 757 | expand_curr_sequ(1); |
---|
| 758 | term: |
---|
| 759 | set_option("echo", &echo); |
---|
| 760 | sequ_is_on = 0; |
---|
| 761 | return; |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | // public functions |
---|
| 765 | |
---|
| 766 | void |
---|
| 767 | get_sxf_names(void) |
---|
| 768 | /* reads and stores names for SXF I/O from madxl.h */ |
---|
| 769 | { |
---|
| 770 | int i = 0; |
---|
| 771 | while (sxf_table_names[i][0] != ' ') |
---|
| 772 | { |
---|
| 773 | add_to_name_list(sxf_table_names[i++], 0, sxf_list); |
---|
| 774 | } |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | void |
---|
| 778 | pro_sxf(struct in_cmd* cmd) |
---|
| 779 | /* controls reading and writing of SXF format files */ |
---|
| 780 | { |
---|
| 781 | struct name_list* nl = cmd->clone->par_names; |
---|
| 782 | struct command_parameter_list* pl = cmd->clone->par; |
---|
| 783 | int pos = name_list_pos("file", nl); |
---|
| 784 | char* filename = NULL; |
---|
| 785 | FILE* inout; |
---|
| 786 | |
---|
| 787 | if (nl->inform[pos]) |
---|
| 788 | { |
---|
| 789 | if ((filename = pl->parameters[pos]->string) == NULL) |
---|
| 790 | { |
---|
| 791 | if (pl->parameters[pos]->call_def != NULL) |
---|
| 792 | filename = pl->parameters[pos]->call_def->string; |
---|
| 793 | } |
---|
| 794 | } |
---|
| 795 | else |
---|
| 796 | { |
---|
| 797 | if (pl->parameters[pos]->call_def != NULL) |
---|
| 798 | filename = pl->parameters[pos]->call_def->string; |
---|
| 799 | } |
---|
| 800 | if (filename == NULL) filename = permbuff("dummy"); |
---|
| 801 | if (strcmp(cmd->tok_list->p[0], "sxfread") == 0) |
---|
| 802 | { |
---|
| 803 | if (down_unit(filename) == 0) return; |
---|
| 804 | sxf_read(cmd->clone); |
---|
| 805 | fclose(in->input_files[in->curr--]); |
---|
| 806 | } |
---|
| 807 | else if (strcmp(cmd->tok_list->p[0], "sxfwrite") == 0) |
---|
| 808 | { |
---|
| 809 | if ((inout = fopen(filename, "w")) == NULL) |
---|
| 810 | { |
---|
| 811 | warning("cannot open output file: ", filename); |
---|
| 812 | return; |
---|
| 813 | } |
---|
| 814 | sxf_write(cmd->clone, inout); |
---|
| 815 | } |
---|
| 816 | } |
---|
| 817 | |
---|