source: PSPA/madxPSPA/src/mad_track.c @ 466

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

import madx-5.01.00

File size: 14.7 KB
Line 
1#include "madx.h"
2
3// private functions
4
5static void
6track_observe(struct in_cmd* cmd)
7{
8  struct name_list* nl = cmd->clone->par_names;
9  struct command_parameter_list* pl = cmd->clone->par;
10  struct node* nodes[2];
11  int pos;
12  if (track_is_on == 0)
13  {
14    warning("track_observe: no TRACK command seen yet,", "ignored");
15    return;
16  }
17  pos = name_list_pos("place", nl);
18  if (get_ex_range(pl->parameters[pos]->string, current_sequ, nodes))
19  {
20    nodes[0]->obs_point = ++curr_obs_points;
21    nodes[0]->obs_orbit = new_double_array(6);
22    nodes[0]->obs_orbit->curr = 6;
23    adjust_beam();
24    if (probe_beam) probe_beam = delete_command(probe_beam);
25    probe_beam = clone_command(current_beam);
26    adjust_probe(track_deltap); /* sets correct gamma, beta, etc. */
27    adjust_rfc(); /* sets freq in rf-cavities from probe */
28    zero_double(orbit0, 6);
29    zero_double(oneturnmat, 36);
30    if (get_option("onepass") == 0)
31    {
32      tmrefo_(&curr_obs_points,orbit0,nodes[0]->obs_orbit->a,oneturnmat);
33      /* closed orbit and one-turn linear transfer map */
34    }
35  }
36  else
37  {
38    warning("track_observe: unknown place,", "ignored");
39    return;
40  }
41}
42
43static void
44track_run(struct in_cmd* cmd)
45{
46  char rout_name[] = "track_run";
47  int e_flag, flag = 1, izero = 0, npart = stored_track_start->curr;
48  int *ibuf1, *ibuf2, *ibuf3;
49  double orbit[6];
50  double *buf1, *buf2, *buf_dxt, *buf_dyt, *buf3, *buf4, buf5, *buf6;
51  struct table* t;
52
53  int turns = command_par_value("turns", cmd->clone);
54
55  if (track_is_on == 0) {
56    warning("track_run: no TRACK command seen yet", "ignored");
57    return;
58  }
59
60  if (npart == 0) {
61    warning("track_run: no START command seen yet", "ignored");
62    return;
63  }
64
65  adjust_beam();
66  if (probe_beam) probe_beam = delete_command(probe_beam);
67  probe_beam = clone_command(current_beam);
68  adjust_probe(track_deltap); /* sets correct gamma, beta, etc. */
69  adjust_rfc(); /* sets freq in rf-cavities from probe */
70  zero_double(orbit0, 6);
71  zero_double(oneturnmat, 36);
72
73  if (get_option("onepass") == 0) {
74    tmrefo_(&izero,orbit0,orbit,oneturnmat);
75    /* closed orbit and one-turn linear transfer map */
76  }
77
78  track_tables_create(cmd);
79
80  /* allocate buffers */
81  ibuf1   = mymalloc(rout_name,npart*sizeof(int));
82  ibuf2   = mymalloc(rout_name,npart*sizeof(int));
83  ibuf3   = mymalloc(rout_name,current_sequ->n_nodes*sizeof(int));
84  buf1    = mymalloc(rout_name,npart*sizeof(double));
85  buf2    = mymalloc(rout_name,6*npart*sizeof(double));
86  buf_dxt = mymalloc(rout_name,npart*sizeof(double));
87  buf_dyt = mymalloc(rout_name,npart*sizeof(double));
88  buf3    = mymalloc(rout_name,6*npart*sizeof(double));
89  buf4    = mymalloc(rout_name,36*sizeof(double));
90  buf6    = mymalloc(rout_name,current_sequ->n_nodes*sizeof(double));
91
92  // run track rountine
93  trrun_(&flag, &turns,orbit0, oneturnmat, ibuf1, ibuf2, buf1, buf2,
94         buf_dxt, buf_dyt, buf3, buf4, &buf5, &e_flag, ibuf3, buf6);
95
96  // summary
97  t = table_register->tables[name_list_pos("tracksumm", table_register->names)];
98  if (get_option("info")) print_table(t);
99  if (get_option("track_dump")) track_tables_dump();
100
101  /* free buffers */
102  myfree(rout_name, ibuf1);
103  myfree(rout_name, ibuf2);
104  myfree(rout_name, ibuf3);
105  myfree(rout_name, buf1);
106  myfree(rout_name, buf2);
107  myfree(rout_name, buf_dxt);
108  myfree(rout_name, buf_dyt);
109  myfree(rout_name, buf3);
110  myfree(rout_name, buf4);
111  myfree(rout_name, buf6);
112  fprintf(prt_file, "\n*****  end of trrun  *****\n");
113}
114
115static void
116track_end(struct in_cmd* cmd)
117{
118  int i;
119  struct node* c_node;
120
121  (void)cmd;
122  if (track_is_on == 0)
123  {
124    warning("track_end: no TRACK command seen yet", "ignored");
125    return;
126  }
127  for (i = 0; i < stored_track_start->curr; i++)
128    stored_track_start->commands[i] =
129      delete_command(stored_track_start->commands[i]);
130  stored_track_start->curr = 0;
131  c_node = current_sequ->ex_start;
132  while(c_node != NULL) /* clean observation points */
133  {
134    c_node->obs_point = 0;
135    c_node->obs_orbit = delete_double_array(c_node->obs_orbit);
136    if (c_node == current_sequ->ex_end)  break;
137    c_node = c_node->next;
138  }
139  track_is_on = 0;
140  fprintf(prt_file, "exit TRACK module\n\n");
141}
142
143static void
144track_ripple(struct in_cmd* cmd)
145{
146  (void)cmd;
147 
148  warning("track_ripple routine is not implemented", "ignored");
149
150  if (track_is_on == 0)
151  {
152    warning("track_ripple: no TRACK command seen yet", "ignored");
153    return;
154  }
155}
156
157static void
158track_track(struct in_cmd* cmd)
159{
160  int k=0, pos, one = 1;
161  struct name_list* nl = cmd->clone->par_names;
162  struct command_parameter_list* pl = cmd->clone->par;
163
164  if (current_sequ == NULL || current_sequ->ex_start == NULL)
165  {
166    warning("sequence not active,", "TRACK ignored");
167    return;
168  }
169  if (attach_beam(current_sequ) == 0)
170    fatal_error("TRACK - sequence without beam:", current_sequ->name);
171  if (track_is_on)
172  {
173    warning("already inside TRACK command group,", "ignored");
174    return;
175  }
176  track_is_on = 1;
177  puts("enter TRACK module");
178  if ((k = get_value(current_command->name,"onepass")) != 0)
179    fprintf(prt_file, "one pass is on\n");
180  set_option("onepass", &k);
181
182  if ((k = get_value(current_command->name,"update")) != 0)
183    fprintf(prt_file, "update is on\n");
184  set_option("update", &k);
185
186
187  if ((k = get_value(current_command->name,"damp")) != 0)
188    fprintf(prt_file, "damp is on\n");
189  set_option("damp", &k);
190  if ((k = get_value(current_command->name,"quantum")) != 0)
191    fprintf(prt_file, "quantum is on\n");
192  set_option("quantum", &k);
193
194
195  if ((k = get_value(current_command->name,"aperture")) != 0)
196    fprintf(prt_file, "aperture tracking is on\n");
197  set_option("aperture", &k);
198  if ((k = get_value(current_command->name,"recloss")) != 0)
199    fprintf(prt_file, "losses recorded\n");
200  set_option("recloss", &k);
201  k = get_value(current_command->name,"dump");
202  set_option("track_dump", &k);
203  k = get_value(current_command->name,"onetable");
204  set_option("onetable", &k);
205  track_deltap=get_value(current_command->name,"deltap");
206  set_variable("track_deltap", &track_deltap);
207  if(track_deltap != 0) fprintf(prt_file, v_format("track_deltap: %F\n"),
208                                track_deltap);
209  curr_obs_points = 1;  /* default: always observe at machine end */
210  pos = name_list_pos("file", nl);
211  if (nl->inform[pos]) set_option("track_dump", &one);
212  if ((track_filename = pl->parameters[pos]->string) == NULL)
213  {
214    if (pl->parameters[pos]->call_def != NULL)
215      track_filename = pl->parameters[pos]->call_def->string;
216    else track_filename = permbuff("dummy");
217  }
218  track_filename = permbuff(track_filename);
219  track_fileext = NULL;
220  pos = name_list_pos("extension", nl);
221  if ((track_fileext = pl->parameters[pos]->string) == NULL)
222  {
223    if (pl->parameters[pos]->call_def != NULL)
224      track_fileext = pl->parameters[pos]->call_def->string;
225    if (track_fileext == NULL)  track_fileext = permbuff("\0");
226  }
227  track_fileext = permbuff(track_fileext);
228}
229
230/**
231 * copies track positions from commands to array
232 * returns number of copied tracks, value <= 0 in case of error
233 *
234 * Used by gettrack
235 */
236static int
237copytrackstoarray(void)
238{
239  int ntracks = 0;/*number of tracks : returned value */
240  int n = 0; /*interator over tracks*/
241  struct command* comm;
242  if (trackstrarpositions)
243  {
244    deletetrackstrarpositions();
245  }
246
247  ntracks = getnumberoftracks();
248  if (ntracks <= 0)
249  {
250    printf("ERROR: copytrackstoarray: number of tracks is 0! Nothing to copy!");
251    return 0;
252  }
253  trackstrarpositions =  (double**)mymalloc("copytrackstoarray",ntracks*sizeof(double*));
254
255  for (n = 0; n < ntracks; n++)
256  {
257    trackstrarpositions[n] = (double*)mymalloc("copytrackstoarray",6*sizeof(double));
258
259    comm = stored_track_start->commands[n];
260    trackstrarpositions[n][0] = command_par_value("x",  comm);
261    trackstrarpositions[n][1] = command_par_value("px", comm);
262    trackstrarpositions[n][2] = command_par_value("y",  comm);
263    trackstrarpositions[n][3] = command_par_value("py", comm);
264    trackstrarpositions[n][4] = command_par_value("t",  comm);
265    trackstrarpositions[n][5] = command_par_value("pt", comm);
266
267  }
268  return ntracks;
269
270}
271
272// public interface
273
274int
275next_start(double* x,double* px,double* y,double* py,double* t,
276           double* deltae,double* fx,double* phix,double* fy,double* phiy,
277           double* ft,double* phit)
278  /* returns the parameters of the next particle to track;
279     0 = none, else count */
280{
281  struct command* comm;
282  if (start_cnt == stored_track_start->curr)
283  {
284    start_cnt = 0; return 0;
285  }
286  comm = stored_track_start->commands[start_cnt];
287  *x = command_par_value("x", comm);
288  *px = command_par_value("px", comm);
289  *y = command_par_value("y", comm);
290  *py = command_par_value("py", comm);
291  *t = command_par_value("t", comm);
292  *deltae = command_par_value("pt", comm);
293  *fx = command_par_value("fx", comm);
294  *phix = command_par_value("phix", comm);
295  *fy = command_par_value("fy", comm);
296  *phiy = command_par_value("phiy", comm);
297  *ft = command_par_value("ft", comm);
298  *phit = command_par_value("phit", comm);
299  return ++start_cnt;
300}
301
302void
303pro_track(struct in_cmd* cmd)
304  /* controls track module */
305{
306  if (current_sequ == NULL || current_sequ->ex_start == NULL)
307  {
308    warning("TRACK, but no active sequence:", "ignored");
309    return;
310  }
311  if (strcmp(cmd->tok_list->p[0], "track") == 0)
312  {
313    track_track(cmd);
314  }
315  if (strcmp(cmd->tok_list->p[0], "dynap") == 0)
316  {
317    track_dynap(cmd);
318  }
319  else if (strcmp(cmd->tok_list->p[0], "endtrack") == 0)
320  {
321    track_end(cmd);
322  }
323  else if (strcmp(cmd->tok_list->p[0], "observe") == 0)
324  {
325    track_observe(cmd);
326  }
327  else if (strcmp(cmd->tok_list->p[0], "run") == 0)
328  {
329    track_run(cmd);
330  }
331  else if (strcmp(cmd->tok_list->p[0], "ripple") == 0)
332  {
333    track_ripple(cmd);
334  }
335  else if (strcmp(cmd->tok_list->p[0], "start") == 0)
336  {
337    track_start(cmd->clone);
338    cmd->clone_flag = 1;
339  }
340}
341
342void
343track_pteigen(double* eigen)
344{
345  int i, j, pos;
346  struct table* t;
347  double tmp;
348  if ((pos = name_list_pos("trackone", table_register->names)) > -1)
349  {
350    t = table_register->tables[pos];
351    if (t->header == NULL)  t->header = new_char_p_array(45);
352    sprintf(c_dum->c, v_format("@ XC               %%le  %F"), orbit0[0]);
353    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
354    sprintf(c_dum->c, v_format("@ PXC              %%le  %F"), orbit0[1]);
355    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
356    sprintf(c_dum->c, v_format("@ YC               %%le  %F"), orbit0[2]);
357    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
358    sprintf(c_dum->c, v_format("@ PYC              %%le  %F"), orbit0[3]);
359    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
360    sprintf(c_dum->c, v_format("@ TC               %%le  %F"), orbit0[4]);
361    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
362    sprintf(c_dum->c, v_format("@ PTC              %%le  %F"), orbit0[5]);
363    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
364    tmp = get_value("beam", "ex");
365    sprintf(c_dum->c, v_format("@ EX               %%le  %F"), tmp);
366    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
367    tmp = get_value("beam", "ey");
368    sprintf(c_dum->c, v_format("@ EY               %%le  %F"), tmp);
369    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
370    tmp = get_value("beam", "et");
371    sprintf(c_dum->c, v_format("@ ET               %%le  %F"), tmp);
372    t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
373    for (i = 0; i < 6; i++)
374    {
375      for (j = 0; j < 6; j++)
376      {
377        sprintf(c_dum->c, v_format("@ E%d%d              %%le  %F"),
378                i+1, j+1, eigen[6*j+i]);
379        t->header->p[t->header->curr++] = tmpbuff(c_dum->c);
380      }
381    }
382  }
383}
384
385void
386track_start(struct command* comm)
387{
388  char name[FNAME_L];
389  if (track_is_on == 0)
390  {
391    warning("track_start: no TRACK command seen yet", "ignored");
392    return;
393  }
394  track_start_cnt++;
395  strcpy(name, "start.");
396  sprintf(c_dum->c, "%d", track_start_cnt);
397  strcat(name, c_dum->c);
398  add_to_command_list(name,comm,stored_track_start,1);
399}
400
401void
402track_tables_create(struct in_cmd* cmd)
403{
404  int i, j;
405  char tab_name[NAME_L];
406  struct table* t;
407  int t_size;
408  int turns = command_par_value("turns", cmd->clone);
409  int ffile = command_par_value("ffile", cmd->clone);
410  if (ffile <= 0) ffile = 1;
411  t_size = turns / ffile + 10;
412  t = make_table("tracksumm", "tracksumm", tracksumm_table_cols,
413                 tracksumm_table_types, 2*stored_track_start->curr);
414  add_to_table_list(t, table_register);
415  if (get_option("recloss"))
416  {
417    t = make_table("trackloss", "trackloss", trackloss_table_cols,
418                   trackloss_table_types, stored_track_start->curr*t_size);
419    add_to_table_list(t, table_register);
420  }
421  if (get_option("onetable"))
422  {
423    t = make_table("trackone", "trackone", trackone_table_cols,
424                   trackone_table_types, stored_track_start->curr*t_size);
425    add_to_table_list(t, table_register);
426  }
427  else
428  {
429    for (i = 0; i < curr_obs_points; i++)
430    {
431      for (j = 0; j < stored_track_start->curr; j++) /* open tables */
432      {
433        sprintf(tab_name, "track.obs%04d.p%04d", i+1, j+1);
434        t = make_table(tab_name, "trackobs", track_table_cols,
435                       track_table_types, t_size);
436        add_to_table_list(t, table_register);
437      }
438    }
439  }
440}
441
442void
443track_tables_dump(void)
444{
445  int j;
446  for (j = 0; j < table_register->names->curr; j++)
447  {
448    if (strstr(table_register->names->names[j], "track.obs")
449        || strcmp(table_register->names->names[j], "trackone") == 0)
450    {
451      strcpy(l_wrk->c, track_filename);
452      strcat(l_wrk->c, &table_register->names->names[j][5]);
453      strcat(l_wrk->c, track_fileext);
454      out_table("track", table_register->tables[j], l_wrk->c);
455    }
456  }
457}
458
459int
460getnumberoftracks(void)
461{
462/*returns number of input tracks */
463  if (stored_track_start == 0x0)
464  {
465    return 0;
466  }
467
468  return stored_track_start->curr;
469
470}
471
472int
473getcurrentcmdname(char* string)
474{
475  if (current_command == 0x0)
476  {
477    return 0;
478  }
479
480  strcpy(string, current_command->name);
481  return strlen(current_command->name);
482
483}
484
485int
486gettrack(int* nt, double* x,double* px,double* y,double* py,double* t,double* pt)
487{
488  /* returns the parameters of track n;
489     0 = none, else count */
490  int n = *nt - 1;
491
492  if ( trackstrarpositions == 0x0 )
493  {
494    copytrackstoarray();
495  }
496  if ( (n<0) || (n >= stored_track_start->curr) )
497  {
498    printf("gettrack: track number %d out of range",n);
499    return 1;
500  }
501
502
503  *x      = trackstrarpositions[n][0];
504  *px     = trackstrarpositions[n][1];
505  *y      = trackstrarpositions[n][2];
506  *py     = trackstrarpositions[n][3];
507  *t      = trackstrarpositions[n][4];
508  *pt     = trackstrarpositions[n][5];
509  return 0;
510}
511
512void
513deletetrackstrarpositions(void)
514{
515  /* deletes the array with track positions */
516  int i;
517  for ( i = 0; i < stored_track_start->curr; i++)
518  {
519    myfree("deletetrackstrarpositions",trackstrarpositions[i]);
520  }
521  myfree("deletetrackstrarpositions",trackstrarpositions);
522
523  trackstrarpositions = 0x0;
524}
525
Note: See TracBrowser for help on using the repository browser.