source: PSPA/madxPSPA/src/mad_var.c @ 430

Last change on this file since 430 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 17.6 KB
Line 
1#include "madx.h"
2
3static struct variable*
4delete_variable(struct variable* var)
5{
6  char rout_name[] = "delete_variable";
7  if (var == NULL)  return NULL;
8  if (stamp_flag && var->stamp != 123456)
9    fprintf(stamp_file, "d_v double delete --> %s\n", var->name);
10  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", var->name);
11  if (var->expr != NULL) delete_expression(var->expr);
12  if (var->string != NULL) myfree(rout_name, var->string);
13  myfree(rout_name, var);
14  return NULL;
15}
16
17static void
18grow_var_list(struct var_list* p)
19{
20  char rout_name[] = "grow_var_list";
21  struct variable** v_loc = p->vars;
22  int j, new = 2*p->max;
23
24  p->max = new;
25  p->vars
26    = (struct variable**) mycalloc(rout_name,new, sizeof(struct variable*));
27  for (j = 0; j < p->curr; j++) p->vars[j] = v_loc[j];
28  myfree(rout_name, v_loc);
29}
30
31#if 0 // not used...
32static void
33dump_variable(struct variable* v)
34{
35  fprintf(prt_file, "=== dumping variable %s\n", v->name);
36}
37#endif
38
39static void
40export_variable(struct variable* var, FILE* file)
41  /* exports variable in mad-X format */
42{
43  int k;
44  *c_dum->c = '\0';
45  if (var->status == 0) var->value = expression_value(var->expr, var->type);
46  if (var->val_type == 0) strcat(c_dum->c, "int ");
47  if (var->type == 0) strcat(c_dum->c, "const ");
48  strcat(c_dum->c, var->name);
49  if (var->type < 2) strcat(c_dum->c, " = ");
50  else               strcat(c_dum->c, " := ");
51  if (var->expr != NULL) strcat(c_dum->c, var->expr->string);
52  else if (var->val_type == 0)
53  {
54    k = var->value; sprintf(c_join->c, "%d", k); strcat(c_dum->c, c_join->c);
55  }
56  else
57  {
58    sprintf(c_join->c, v_format("%F"), var->value);
59    strcat(c_dum->c, supp_tb(c_join->c));
60  }
61  write_nice(c_dum->c, file);
62}
63
64static void
65export_var_8(struct variable* var, FILE* file)
66  /* exports variable in mad-8 format */
67{
68  int k;
69  *c_dum->c = '\0';
70  if (var->status == 0) var->value = expression_value(var->expr, var->type);
71  if (var->type == 0)
72  {
73    strcat(c_dum->c, var->name);
74    strcat(c_dum->c, ": constant = ");
75  }
76  else
77  {
78    strcat(c_dum->c, var->name);
79    if (var->type < 2) strcat(c_dum->c, " = ");
80    else               strcat(c_dum->c, " := ");
81  }
82  if (var->expr != NULL) strcat(c_dum->c, var->expr->string);
83  else if (var->val_type == 0)
84  {
85    k = var->value; sprintf(c_join->c, v_format("%I"), k);
86    strcat(c_dum->c, c_join->c);
87  }
88  else
89  {
90    sprintf(c_join->c, v_format("%F"), var->value);
91    strcat(c_dum->c, supp_tb(c_join->c));
92  }
93  write_nice_8(c_dum->c, file);
94}
95
96static int
97predef_var(struct variable* var)
98  /* return 1 for predefined variable, else 0 */
99{
100  int pos = name_list_pos(var->name, variable_list->list);
101  return (pos < start_var ? 1 : 0) ;
102}
103
104static void
105set_sub_variable(char* comm, char* par, struct in_cmd* cmd)
106{
107  char* p;
108  struct element* el;
109  struct command *command, *keep_beam = current_beam;
110  int end, start = cmd->decl_start, t_num, exp_type;
111  double val = 0;
112  for (t_num = start; t_num < cmd->tok_list->curr; t_num++)
113    if (*(cmd->tok_list->p[t_num]) == ',') break;
114  exp_type = loc_expr(cmd->tok_list->p, t_num,
115                      start, &end);
116  if (exp_type == 1) /* literal constant */
117    val = simple_double(cmd->tok_list->p, start, end);
118  else if (polish_expr(end + 1 - start, &cmd->tok_list->p[start]) == 0)
119    val = polish_value(deco, join(&cmd->tok_list->p[start], end + 1 - start));
120  if (strncmp(comm, "beam", 4) == 0)
121  {
122    command = current_beam = find_command("default_beam", beam_list);
123    if ((p = strchr(comm, '%')) != NULL)
124    {
125      if ((current_beam = find_command(++p, beam_list)) == NULL)
126        current_beam = command;
127    }
128    set_command_par_value(par, current_beam, val);
129  }
130  else if ((el = find_element(comm, element_list)) != NULL)
131    set_command_par_value(par, el->def, val);
132  else if ((command = find_command(comm, stored_commands)) != NULL)
133    set_command_par_value(par, command, val);
134  else if ((command = find_command(comm, beta0_list)) != NULL)
135    set_command_par_value(par, command, val);
136  else if ((command = find_command(comm, defined_commands)) != NULL)
137    set_command_par_value(par, command, val);
138  current_beam = keep_beam;
139}
140
141// public interface
142
143void
144get_defined_constants(void)
145{
146  /* reads + stores the constants defined in madxdict.h */
147  supp_char('\n', constant_def);
148  pro_input(constant_def);
149  start_var = variable_list->curr;
150}
151
152struct var_list*
153delete_var_list(struct var_list* varl)
154{
155  char rout_name[] = "delete_var_list";
156  if (varl == NULL) return NULL;
157  if (stamp_flag && varl->stamp != 123456)
158    fprintf(stamp_file, "d_v_l double delete --> %s\n", varl->name);
159  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", varl->name);
160  if (varl->list != NULL) delete_name_list(varl->list);
161  if (varl->vars != NULL) myfree(rout_name, varl->vars);
162  myfree(rout_name, varl);
163  return NULL;
164}
165
166struct variable*
167find_variable(char* name, struct var_list* varl)
168{
169  int pos;
170  if ((pos = name_list_pos(name, varl->list)) < 0)
171    return NULL;
172  return varl->vars[pos];
173}
174
175double
176variable_value(struct variable* var)
177{
178  int k;
179  double val = zero;
180  if (var->type < 2 && var->status > 0) val = var->value;
181  else if(var->expr == NULL) val = var->value;
182  else
183  {
184    var->value = val = expression_value(var->expr, var->type);
185    var->status = 1;
186  }
187  if (var->val_type == 0) /* int */
188  {
189    k = val; val = k;
190  }
191  return val;
192}
193
194struct var_list*
195clone_var_list(struct var_list* vl)
196{
197  int i, l = vl->curr > 0 ? vl->curr : 1;
198  struct var_list* clone;
199  clone = new_var_list(l);
200  strcpy(clone->name, vl->name);
201  clone->list = clone_name_list(vl->list);
202  for (i = 0; i < vl->curr; i++) clone->vars[i] = vl->vars[i];
203  clone->curr = vl->curr;
204  return clone;
205}
206
207struct variable*
208new_variable(char* name, double val, int val_type, int type, struct expression* expr, char* string)
209{
210  char rout_name[] = "new_variable";
211  struct variable* var =
212    (struct variable*) mycalloc(rout_name,1, sizeof(struct variable));
213  strcpy(var->name, name);
214  var->stamp = 123456;
215  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", var->name);
216  var->value = val;
217  var->type = type;
218  var->val_type = val_type;
219  if ((var->expr = expr) == NULL) var->status = 1;
220  if (string != NULL) var->string = tmpbuff(string);
221  return var;
222}
223
224struct var_list*
225new_var_list(int length)
226{
227  char rout_name[] = "new_var_list";
228  struct var_list* var
229    = (struct var_list*) mycalloc(rout_name,1, sizeof(struct var_list));
230  strcpy(var->name, "var_list");
231  var->stamp = 123456;
232  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", var->name);
233  var->list = new_name_list(var->name, length);
234  var->vars
235    = (struct variable**) mycalloc(rout_name,length, sizeof(struct variable*));
236  var->max = length;
237  return var;
238}
239
240char*
241get_varstring(char* name)
242{
243  struct variable* var;
244  char *ret; // *p, not used
245  ret = NULL;
246  mycpy(c_dum->c, name);
247  if (strstr(c_dum->c, "->") == NULL) /* variable */ // (p = not used
248    if ((var = find_variable(c_dum->c, variable_list)) != NULL)
249      ret = var->string;
250
251  return ret;
252}
253
254void
255write_vars(struct var_list* varl, struct command_list* cl, FILE* file)
256{
257  int i;
258  for (i = 0; i < varl->curr; i++)
259  {
260    if (predef_var(varl->vars[i]) == 0
261        && pass_select_list(varl->vars[i]->name, cl))
262      export_variable(varl->vars[i], file);
263  }
264}
265
266void
267write_vars_8(struct var_list* varl, struct command_list* cl, FILE* file)
268{
269  int i;
270  for (i = 0; i < varl->curr; i++)
271  {
272    if (predef_var(varl->vars[i]) == 0
273        && pass_select_list(varl->vars[i]->name, cl))
274      export_var_8(varl->vars[i], file);
275  }
276}
277
278char*
279make_string_variable(char* string)
280  /* creates + stores a variable containing a character string */
281{
282  char* name = get_new_name();
283  struct variable* var = new_variable(name, zero, 3, 0, NULL, string);
284  add_to_var_list(var, variable_list, 0);
285  return var->name;
286}
287
288void
289enter_variable(struct in_cmd* cmd) /* stores variable contained in cmd */
290{
291  struct variable* var;
292  struct expression* expr = NULL;
293  int k, end, type = 0, name_pos = 0, start = cmd->decl_start, val_type = 0;
294  double val = 0;
295  char comm[NAME_L];
296  char par[NAME_L];
297  char* name;
298  char *p, *n, *q = comm;
299  int exp_type = loc_expr(cmd->tok_list->p, cmd->tok_list->curr,
300                          start, &end);
301  switch (cmd->sub_type)
302  {
303    case 2:
304    case 3:
305      val_type = 0;
306      type = 0;
307      name_pos = 2;
308      break;
309    case 4:
310    case 5:
311      val_type = 1;
312      type = 0;
313      name_pos = 2;
314      break;
315    case 6:
316      val_type = 0;
317      type = 1;
318      name_pos = 1;
319      break;
320    case 7:
321      val_type = 0;
322      type = 2;
323      name_pos = 1;
324      break;
325    case 8:
326      val_type = 1;
327      type = 1;
328      name_pos = 1;
329      break;
330    case 9:
331      val_type = 1;
332      type = 2;
333      name_pos = 1;
334      break;
335    case 10:
336    case 11:
337      val_type = 1;
338      type = 0;
339      name_pos = 1;
340      break;
341    case 12:
342      val_type = 1;
343      type = 1;
344      name_pos = 0;
345      break;
346    case 13:
347      val_type = 1;
348      type = 2;
349      name_pos = 0;
350      break;
351    default:
352      fatal_error("illegal command sub_type in:",
353                  join(cmd->tok_list->p, cmd->tok_list->curr));
354  }
355  n = name = permbuff(cmd->tok_list->p[name_pos]);
356  if (exp_type == 0)
357  {
358    warning("illegal expression set to 0 in:",
359            join_b(cmd->tok_list->p, cmd->tok_list->curr));
360  }
361  else
362  {
363    if ((p = strstr(name,"->")) != NULL) /* element or command parameter */
364    {
365      while (n < p)  *(q++) = *(n++);
366      *q = '\0';
367      q = par; n++; n++;
368      while (*n != '\0')  *(q++) = *(n++);
369      *q = '\0';
370      set_sub_variable(comm, par, cmd);
371    }
372    else if ((var = find_variable(name, variable_list)) != 0
373             && var->type == 0)
374    {
375      warning("ignored: attempt to redefine constant:", var->name);
376    }
377    else if (exp_type == 1) /* literal constant */
378    {
379      val = simple_double(cmd->tok_list->p, start, end);
380      var = new_variable(name, val, val_type, type, NULL, NULL);
381      add_to_var_list(var, variable_list, 1);
382    }
383    else
384    {
385      if (polish_expr(end + 1 - start, &cmd->tok_list->p[start]) == 0)
386      {
387        if (type == 2) /* deferred: expression kept */
388        {
389          expr = new_expression(join(&cmd->tok_list->p[start], end + 1 - start), deco);
390          val = 0; // LD 2012.10.16: drop warning due to expression_value(expr, type);
391        }
392        else
393        {
394          expr = NULL;
395          val = polish_value(deco, join(&cmd->tok_list->p[start], end + 1 - start));
396        }
397        if (val_type == 0)
398        {
399          if (fabs(val) < 2.e9)  k = val;
400          else                   k = 0;
401          val = k;
402        }
403        var = new_variable(name, val, val_type, type, expr, NULL);
404        add_to_var_list(var, variable_list, 1);
405      }
406      else
407      {
408        warning("illegal expression set to 0 in:",
409                join_b(cmd->tok_list->p, cmd->tok_list->curr));
410      }
411    }
412  }
413}
414
415void
416add_to_var_list( /* adds variable to alphabetic variable list */
417  struct variable* var, struct var_list* varl, int flag)
418  /* flag = 0: undefined reference in expression, 1: definition
419     2: separate list, do not drop variable */
420{
421  int pos; // , j; not used
422
423  if ((pos = name_list_pos(var->name, varl->list)) > -1)
424  {
425    if (flag == 1)
426    {
427      if (varl->list->inform[pos] == 1)
428      {
429        put_info(var->name, "redefined");
430/*
431  printf("Old Value:\n");
432  export_variable(varl->vars[pos], stdout);
433  printf("New Value:\n");
434  export_variable(var, stdout);
435  printf("File %s line %d\n",filenames[in->curr], currentline[in->curr] );
436*/
437      }
438      else varl->list->inform[pos] = flag;
439    }
440    if (flag < 2) delete_variable(varl->vars[pos]);
441    varl->vars[pos] = var;
442  }
443  else
444  {
445    if (varl->curr == varl->max) grow_var_list(varl);
446    add_to_name_list(permbuff(var->name), flag, varl->list); // j = not used
447    varl->vars[varl->curr++] = var;
448  }
449}
450
451void
452set_stringvar(char* name, char* string)
453{
454  /* sets variable name->string to string */
455//  char* p;
456  struct variable* var;
457  mycpy(c_dum->c, name);
458  if (strstr(c_dum->c, "->") == NULL) /* variable */ // (p = not used
459  {
460    if ((var = find_variable(c_dum->c, variable_list)) != NULL)
461    {
462      if (var->type == 3) var->string = string;
463    }
464    else
465    {
466      var = new_variable(c_dum->c, zero, 0, 3, NULL, string);
467      add_to_var_list(var, variable_list, 1);
468    }
469  }
470}
471
472void
473print_global(double delta)
474{
475  char tmp[NAME_L], trad[4];
476  double alfa = get_value("probe", "alfa");
477  double freq0 = get_value("probe", "freq0");
478  double gamma = get_value("probe", "gamma");
479  double beta = get_value("probe", "beta");
480  double circ = get_value("probe", "circ");
481  double bcurrent = get_value("probe", "bcurrent");
482  double npart = get_value("probe", "npart");
483  double energy = get_value("probe", "energy");
484  int kbunch = get_value("probe", "kbunch");
485  int rad = get_value("probe", "radiate");
486  double gamtr = zero, t0 = zero, eta;
487
488  (void)delta;
489  get_string("probe", "particle", tmp);
490  if (rad) strcpy(trad, "T");
491  else     strcpy(trad, "F");
492  if (alfa > zero) gamtr = sqrt(one / alfa);
493  else if (alfa < zero) gamtr = sqrt(-one / alfa);
494  if (freq0 > zero) t0 = one / freq0;
495  eta = alfa - one / (gamma*gamma);
496  puts(" ");
497  printf(" Global parameters for %ss, radiate = %s:\n\n",
498         tmp, trad);
499  printf(v_format(" C         %F m          f0        %F MHz\n"),circ, freq0);
500  printf(v_format(" T0        %F musecs     alfa      %F \n"), t0, alfa);
501  printf(v_format(" eta       %F            gamma(tr) %F \n"), eta, gamtr);
502  printf(v_format(" Bcurrent  %F A/bunch    Kbunch    %I \n"),
503         bcurrent, kbunch);
504  printf(v_format(" Npart     %F /bunch     Energy    %F GeV \n"),
505         npart,energy);
506  printf(v_format(" gamma     %F            beta      %F\n"),
507         gamma, beta);
508}
509
510#if 0 // not used
511int
512vary_name(char* name, int* name_l, int* index)
513  /* returns the variable name */
514{
515  int pos, ncp, nbl, len;
516  char* v_name;
517  struct name_list* nl;
518  struct command* comm;
519  struct command_parameter_list* pl;
520  comm = stored_match_var->commands[*index];
521  nl = comm->par_names;
522  pl = comm->par;
523  pos = name_list_pos("name", nl);
524  v_name = pl->parameters[pos]->string;
525  len = strlen(v_name);
526  ncp = len < *name_l ? len : *name_l; // min(len, *name_l)
527  nbl = *name_l - ncp;
528  strncpy(name, v_name, ncp);
529  return 1;
530}
531#endif
532
533#if 0 // not used...
534double
535sss_variable(char* name)
536{
537  char comm[NAME_L];
538  char par[NAME_L];
539  double val = zero;
540  struct variable* var;
541  struct element* el;
542  struct command* cmd;
543  char *p, *n = c_dum->c, *q = comm;
544  mycpy(c_dum->c, name);
545  if ((p = strstr(c_dum->c, "->")) == NULL) // variable
546  {
547    if ((var = find_variable(c_dum->c, variable_list)) != NULL)
548      val = variable_value(var);
549  }
550  else // element or command parameter
551  {
552    while (n < p)  *(q++) = *(n++);
553    *q = '\0';
554    q = par; n++; n++;
555    while (*n != '\0')  *(q++) = *(n++);
556    *q = '\0';
557    if ((el = find_element(comm, element_list)) != NULL)
558      val = command_par_value(par, el->def);
559    else if ((cmd = find_command(comm, stored_commands)) != NULL)
560      val = command_par_value(par, cmd);
561    else if ((cmd = find_command(comm, beta0_list)) != NULL)
562      val = command_par_value(par, cmd);
563    else if ((cmd = find_command(comm, defined_commands)) != NULL)
564      val = command_par_value(par, cmd);
565  }
566  return val;
567}
568#endif
569
570// public interface (used by Fortran)
571
572double
573get_variable(char* name)
574{
575  char comm[NAME_L];
576  char par[NAME_L];
577  double val = zero;
578  struct variable* var;
579  struct element* el;
580  struct command* cmd;
581  char *p, *n = c_dum->c, *q = comm;
582  mycpy(c_dum->c, name);
583  if ((p = strstr(c_dum->c, "->")) == NULL) /* variable */
584  {
585    if ((var = find_variable(c_dum->c, variable_list)) != NULL)
586      val = variable_value(var);
587  }
588  else /* element or command parameter */
589  {
590    while (n < p)  *(q++) = *(n++);
591    *q = '\0';
592    q = par; n++; n++;
593    while (*n != '\0')  *(q++) = *(n++);
594    *q = '\0';
595    if ((el = find_element(comm, element_list)) != NULL)
596      val = command_par_value(par, el->def);
597    else if ((cmd = find_command(comm, stored_commands)) != NULL)
598      val = command_par_value(par, cmd);
599    else if ((cmd = find_command(comm, beta0_list)) != NULL)
600      val = command_par_value(par, cmd);
601    else if ((cmd = find_command(comm, defined_commands)) != NULL)
602      val = command_par_value(par, cmd);
603  }
604  return val;
605}
606
607void
608set_variable(char* name, double* value)
609{
610  /* sets variable name to value */
611  char comm[NAME_L];
612  char par[NAME_L];
613  struct variable* var;
614  double val = *value;
615  struct element* el;
616  struct command* cmd;
617  char *p, *n = c_dum->c, *q = comm;
618  mycpy(c_dum->c, name);
619  if ((p = strstr(c_dum->c, "->")) == NULL) /* variable */
620  {
621    if ((var = find_variable(c_dum->c, variable_list)) != NULL)
622    {
623      if (var->type == 0)
624        warning("ignored: attempt to redefine constant:", var->name);
625      else if (var->type < 3)
626      {
627        var->value = val;
628        var->type = 1;
629        if (var->expr != NULL)  var->expr = delete_expression(var->expr);
630      }
631    }
632    else
633    {
634      var = new_variable(c_dum->c, val, 1, 1, NULL, NULL);
635      add_to_var_list(var, variable_list, 1);
636    }
637  }
638  else /* element or command parameter */
639  {
640    while (n < p)  *(q++) = *(n++);
641    *q = '\0';
642    q = par; n++; n++;
643    while (*n != '\0')  *(q++) = *(n++);
644    *q = '\0';
645    if ((el = find_element(comm, element_list)) != NULL)
646      set_command_par_value(par, el->def, val);
647    else if ((cmd = find_command(comm, stored_commands)) != NULL)
648      set_command_par_value(par, cmd, val);
649    else if ((cmd = find_command(comm, beta0_list)) != NULL)
650      set_command_par_value(par, cmd, val);
651    else if ((cmd = find_command(comm, defined_commands)) != NULL)
652      set_command_par_value(par, cmd, val);
653  }
654}
655
Note: See TracBrowser for help on using the repository browser.