1 | #include "madx.h" |
---|
2 | |
---|
3 | // private functions |
---|
4 | |
---|
5 | static void |
---|
6 | fill_beta0(struct command* beta0, struct node* node) |
---|
7 | { |
---|
8 | /* makes uses of fact that beta0 and twiss_table have the same variables |
---|
9 | at the same place (+2) up to energy inclusive */ |
---|
10 | struct command_parameter_list* pl = beta0->par; |
---|
11 | struct name_list* nl = beta0->par_names; |
---|
12 | int i = -1, pos; |
---|
13 | if (twiss_table == NULL) return; |
---|
14 | for (pos = 0; pos < twiss_table->curr; pos++) |
---|
15 | { |
---|
16 | if (twiss_table->p_nodes[pos] == node) break; |
---|
17 | } |
---|
18 | if (pos < twiss_table->curr) |
---|
19 | { |
---|
20 | do |
---|
21 | { |
---|
22 | i++; |
---|
23 | pl->parameters[i]->double_value = twiss_table->d_cols[i+3][pos]; |
---|
24 | /* if (strstr(nl->names[i], "mu")) pl->parameters[i]->double_value *= twopi; frs 19.10.2006*/ |
---|
25 | } |
---|
26 | while (strcmp(nl->names[i], "energy") != 0); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | static void |
---|
31 | exec_savebeta(void) |
---|
32 | /* stores twiss values in a beta0 structure */ |
---|
33 | { |
---|
34 | struct name_list* nl; |
---|
35 | struct command_parameter_list* pl; |
---|
36 | struct node* nodes[2]; |
---|
37 | struct command* beta0; |
---|
38 | char* label; |
---|
39 | int i, pos; |
---|
40 | for (i = 0; i < savebeta_list->curr; i++) |
---|
41 | { |
---|
42 | nl = savebeta_list->commands[i]->par_names; |
---|
43 | pl = savebeta_list->commands[i]->par; |
---|
44 | pos = name_list_pos("label", nl); |
---|
45 | label = pl->parameters[pos]->string; |
---|
46 | if (find_command(label, beta0_list) == NULL) /* fill only once */ |
---|
47 | { |
---|
48 | pos = name_list_pos("sequence", nl); |
---|
49 | if (nl->inform[pos] == 0 || strcmp(pl->parameters[pos]->string, current_sequ->name) == 0) |
---|
50 | { |
---|
51 | pos = name_list_pos("place", nl); |
---|
52 | if (get_ex_range(pl->parameters[pos]->string, current_sequ, nodes)) |
---|
53 | { |
---|
54 | pos = name_list_pos("beta0", defined_commands->list); |
---|
55 | beta0 = clone_command(defined_commands->commands[pos]); |
---|
56 | fill_beta0(beta0, nodes[0]); |
---|
57 | add_to_command_list(label, beta0, beta0_list, 0); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | static void |
---|
65 | fill_twiss_header(struct table* t) |
---|
66 | /* puts beam parameters etc. at start of twiss table */ |
---|
67 | { |
---|
68 | int i, pos, h_length = 39; /* change adding header lines ! */ |
---|
69 | double dtmp; |
---|
70 | struct table* s; |
---|
71 | char tmp[16]; |
---|
72 | |
---|
73 | if (t == NULL) return; |
---|
74 | /* ATTENTION: if you add header lines, augment h_length accordingly */ |
---|
75 | if (t->header == NULL) t->header = new_char_p_array(h_length); |
---|
76 | strcpy(tmp, t->org_sequ->name); |
---|
77 | sprintf(c_dum->c, v_format("@ SEQUENCE %%%02ds \"%s\""), |
---|
78 | strlen(tmp),stoupper(tmp)); |
---|
79 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
80 | i = get_string("beam", "particle", tmp); |
---|
81 | sprintf(c_dum->c, v_format("@ PARTICLE %%%02ds \"%s\""), |
---|
82 | i, stoupper(tmp)); |
---|
83 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
84 | dtmp = get_value("beam", "mass"); |
---|
85 | sprintf(c_dum->c, v_format("@ MASS %%le %F"), dtmp); |
---|
86 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
87 | dtmp = get_value("beam", "charge"); |
---|
88 | sprintf(c_dum->c, v_format("@ CHARGE %%le %F"), dtmp); |
---|
89 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
90 | dtmp = get_value("beam", "energy"); |
---|
91 | sprintf(c_dum->c, v_format("@ ENERGY %%le %F"), dtmp); |
---|
92 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
93 | dtmp = get_value("beam", "pc"); |
---|
94 | sprintf(c_dum->c, v_format("@ PC %%le %F"), dtmp); |
---|
95 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
96 | dtmp = get_value("beam", "gamma"); |
---|
97 | sprintf(c_dum->c, v_format("@ GAMMA %%le %F"), dtmp); |
---|
98 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
99 | dtmp = get_value("beam", "kbunch"); |
---|
100 | sprintf(c_dum->c, v_format("@ KBUNCH %%le %F"), dtmp); |
---|
101 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
102 | dtmp = get_value("beam", "bcurrent"); |
---|
103 | sprintf(c_dum->c, v_format("@ BCURRENT %%le %F"), dtmp); |
---|
104 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
105 | dtmp = get_value("beam", "sige"); |
---|
106 | sprintf(c_dum->c, v_format("@ SIGE %%le %F"), dtmp); |
---|
107 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
108 | dtmp = get_value("beam", "sigt"); |
---|
109 | sprintf(c_dum->c, v_format("@ SIGT %%le %F"), dtmp); |
---|
110 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
111 | dtmp = get_value("beam", "npart"); |
---|
112 | sprintf(c_dum->c, v_format("@ NPART %%le %F"), dtmp); |
---|
113 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
114 | dtmp = get_value("beam", "ex"); |
---|
115 | sprintf(c_dum->c, v_format("@ EX %%le %F"), dtmp); |
---|
116 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
117 | dtmp = get_value("beam", "ey"); |
---|
118 | sprintf(c_dum->c, v_format("@ EY %%le %F"), dtmp); |
---|
119 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
120 | dtmp = get_value("beam", "et"); |
---|
121 | sprintf(c_dum->c, v_format("@ ET %%le %F"), dtmp); |
---|
122 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
123 | if ((pos = name_list_pos("summ", table_register->names)) > -1) |
---|
124 | { |
---|
125 | s = table_register->tables[pos]; |
---|
126 | pos = name_list_pos("length", s->columns); |
---|
127 | dtmp = s->d_cols[pos][0]; |
---|
128 | sprintf(c_dum->c, v_format("@ LENGTH %%le %F"), dtmp); |
---|
129 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
130 | pos = name_list_pos("alfa", s->columns); |
---|
131 | dtmp = s->d_cols[pos][0]; |
---|
132 | sprintf(c_dum->c, v_format("@ ALFA %%le %F"), dtmp); |
---|
133 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
134 | pos = name_list_pos("orbit5", s->columns); |
---|
135 | dtmp = s->d_cols[pos][0]; |
---|
136 | sprintf(c_dum->c, v_format("@ ORBIT5 %%le %F"), dtmp); |
---|
137 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
138 | pos = name_list_pos("gammatr", s->columns); |
---|
139 | dtmp = s->d_cols[pos][0]; |
---|
140 | sprintf(c_dum->c, v_format("@ GAMMATR %%le %F"), dtmp); |
---|
141 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
142 | pos = name_list_pos("q1", s->columns); |
---|
143 | dtmp = s->d_cols[pos][0]; |
---|
144 | sprintf(c_dum->c, v_format("@ Q1 %%le %F"), dtmp); |
---|
145 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
146 | pos = name_list_pos("q2", s->columns); |
---|
147 | dtmp = s->d_cols[pos][0]; |
---|
148 | sprintf(c_dum->c, v_format("@ Q2 %%le %F"), dtmp); |
---|
149 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
150 | pos = name_list_pos("dq1", s->columns); |
---|
151 | dtmp = s->d_cols[pos][0]; |
---|
152 | sprintf(c_dum->c, v_format("@ DQ1 %%le %F"), dtmp); |
---|
153 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
154 | pos = name_list_pos("dq2", s->columns); |
---|
155 | dtmp = s->d_cols[pos][0]; |
---|
156 | sprintf(c_dum->c, v_format("@ DQ2 %%le %F"), dtmp); |
---|
157 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
158 | pos = name_list_pos("dxmax", s->columns); |
---|
159 | dtmp = s->d_cols[pos][0]; |
---|
160 | sprintf(c_dum->c, v_format("@ DXMAX %%le %F"), dtmp); |
---|
161 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
162 | pos = name_list_pos("dymax", s->columns); |
---|
163 | dtmp = s->d_cols[pos][0]; |
---|
164 | sprintf(c_dum->c, v_format("@ DYMAX %%le %F"), dtmp); |
---|
165 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
166 | pos = name_list_pos("xcomax", s->columns); |
---|
167 | dtmp = s->d_cols[pos][0]; |
---|
168 | sprintf(c_dum->c, v_format("@ XCOMAX %%le %F"), dtmp); |
---|
169 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
170 | pos = name_list_pos("ycomax", s->columns); |
---|
171 | dtmp = s->d_cols[pos][0]; |
---|
172 | sprintf(c_dum->c, v_format("@ YCOMAX %%le %F"), dtmp); |
---|
173 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
174 | pos = name_list_pos("betxmax", s->columns); |
---|
175 | dtmp = s->d_cols[pos][0]; |
---|
176 | sprintf(c_dum->c, v_format("@ BETXMAX %%le %F"), dtmp); |
---|
177 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
178 | pos = name_list_pos("betymax", s->columns); |
---|
179 | dtmp = s->d_cols[pos][0]; |
---|
180 | sprintf(c_dum->c, v_format("@ BETYMAX %%le %F"), dtmp); |
---|
181 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
182 | pos = name_list_pos("xcorms", s->columns); |
---|
183 | dtmp = s->d_cols[pos][0]; |
---|
184 | sprintf(c_dum->c, v_format("@ XCORMS %%le %F"), dtmp); |
---|
185 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
186 | pos = name_list_pos("ycorms", s->columns); |
---|
187 | dtmp = s->d_cols[pos][0]; |
---|
188 | sprintf(c_dum->c, v_format("@ YCORMS %%le %F"), dtmp); |
---|
189 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
190 | pos = name_list_pos("dxrms", s->columns); |
---|
191 | dtmp = s->d_cols[pos][0]; |
---|
192 | sprintf(c_dum->c, v_format("@ DXRMS %%le %F"), dtmp); |
---|
193 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
194 | pos = name_list_pos("dyrms", s->columns); |
---|
195 | dtmp = s->d_cols[pos][0]; |
---|
196 | sprintf(c_dum->c, v_format("@ DYRMS %%le %F"), dtmp); |
---|
197 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
198 | pos = name_list_pos("deltap", s->columns); |
---|
199 | dtmp = s->d_cols[pos][0]; |
---|
200 | sprintf(c_dum->c, v_format("@ DELTAP %%le %F"), dtmp); |
---|
201 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
202 | pos = name_list_pos("synch_1", s->columns); |
---|
203 | dtmp = s->d_cols[pos][0]; |
---|
204 | sprintf(c_dum->c, v_format("@ SYNCH_1 %%le %F"), dtmp); |
---|
205 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
206 | pos = name_list_pos("synch_2", s->columns); |
---|
207 | dtmp = s->d_cols[pos][0]; |
---|
208 | sprintf(c_dum->c, v_format("@ SYNCH_2 %%le %F"), dtmp); |
---|
209 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
210 | pos = name_list_pos("synch_3", s->columns); |
---|
211 | dtmp = s->d_cols[pos][0]; |
---|
212 | sprintf(c_dum->c, v_format("@ SYNCH_3 %%le %F"), dtmp); |
---|
213 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
214 | pos = name_list_pos("synch_4", s->columns); |
---|
215 | dtmp = s->d_cols[pos][0]; |
---|
216 | sprintf(c_dum->c, v_format("@ SYNCH_4 %%le %F"), dtmp); |
---|
217 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
218 | pos = name_list_pos("synch_5", s->columns); |
---|
219 | dtmp = s->d_cols[pos][0]; |
---|
220 | sprintf(c_dum->c, v_format("@ SYNCH_5 %%le %F"), dtmp); |
---|
221 | t->header->p[t->header->curr++] = tmpbuff(c_dum->c); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | static void |
---|
226 | pro_embedded_twiss(struct command* current_global_twiss) |
---|
227 | /* controls twiss embedded module */ |
---|
228 | { |
---|
229 | struct command* keep_beam = current_beam; |
---|
230 | struct command* keep_twiss; |
---|
231 | struct name_list* nl = current_twiss->par_names; |
---|
232 | struct command_parameter_list* pl = current_twiss->par; |
---|
233 | struct int_array* tarr; |
---|
234 | struct int_array* dummy_arr; /* for the new signature of the twiss() Fortran function*/ |
---|
235 | struct table* twiss_tb; |
---|
236 | struct table* keep_table = NULL; |
---|
237 | char *filename = NULL, *name, *table_name, *sector_name; |
---|
238 | char *table_embedded_name; |
---|
239 | double tol,tol_keep; |
---|
240 | double betx,bety,alfx,mux,alfy,muy,x,px,y,py,t,pt,dx,dpx,dy,dpy,wx, |
---|
241 | phix,dmux,wy,phiy,dmuy,ddx,ddpx,ddy,ddpy, |
---|
242 | r11,r12,r21,r22,s; |
---|
243 | int i, jt=0, l, lp, k_orb = 0, u_orb = 0, pos, k = 1; |
---|
244 | int w_file, beta_def, err = 0, inval = 1,chrom_flg; // ks, not used |
---|
245 | int keep_info = get_option("info"); |
---|
246 | |
---|
247 | /* Set embedded_flag */ |
---|
248 | |
---|
249 | embedded_flag = 1; |
---|
250 | |
---|
251 | i = keep_info * get_option("twiss_print"); |
---|
252 | set_option("info", &i); |
---|
253 | |
---|
254 | if(get_option("twiss_print")) |
---|
255 | fprintf(prt_file, "enter Twiss module\n"); |
---|
256 | |
---|
257 | /* |
---|
258 | start command decoding |
---|
259 | */ |
---|
260 | pos = name_list_pos("sequence", nl); |
---|
261 | if(nl->inform[pos]) /* sequence specified */ |
---|
262 | { |
---|
263 | name = pl->parameters[pos]->string; |
---|
264 | if ((lp = name_list_pos(name, sequences->list)) > -1) |
---|
265 | current_sequ = sequences->sequs[lp]; |
---|
266 | else |
---|
267 | { |
---|
268 | warning("unknown sequence ignored:", name); |
---|
269 | return; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | if (current_sequ == NULL || current_sequ->ex_start == NULL) { |
---|
274 | warning("sequence not active,", "Twiss ignored"); |
---|
275 | return; |
---|
276 | } |
---|
277 | |
---|
278 | if (attach_beam(current_sequ) == 0) |
---|
279 | fatal_error("TWISS - sequence without beam:", current_sequ->name); |
---|
280 | |
---|
281 | if (!current_sequ->tw_table) { |
---|
282 | warning("no TWISS table present", "TWISS command ignored"); |
---|
283 | return; |
---|
284 | } |
---|
285 | |
---|
286 | table_name = current_sequ->tw_table->name; |
---|
287 | table_embedded_name = "embedded_twiss_table"; |
---|
288 | |
---|
289 | if (get_value(current_command->name,"sectormap") != 0) // (ks = not used |
---|
290 | { |
---|
291 | set_option("twiss_sector", &k); |
---|
292 | pos = name_list_pos("sectorfile", nl); |
---|
293 | if(nl->inform[pos]) |
---|
294 | { |
---|
295 | if ((sector_name = pl->parameters[pos]->string) == NULL) |
---|
296 | sector_name = pl->parameters[pos]->call_def->string; |
---|
297 | } |
---|
298 | else sector_name = pl->parameters[pos]->call_def->string; |
---|
299 | if ((sec_file = fopen(sector_name, "w")) == NULL) |
---|
300 | fatal_error("cannot open output file:", sector_name); |
---|
301 | } |
---|
302 | |
---|
303 | /* Find index to the twiss table */ |
---|
304 | |
---|
305 | if((pos = name_list_pos(table_name, table_register->names)) > -1) |
---|
306 | { |
---|
307 | twiss_tb = table_register->tables[pos]; |
---|
308 | if (twiss_tb->origin ==1) return; /* table is read, has no node pointers */ |
---|
309 | for (jt = 0; jt < twiss_tb->curr; jt++) |
---|
310 | { |
---|
311 | if (twiss_tb->p_nodes[jt] == current_sequ->range_start) break; |
---|
312 | } |
---|
313 | } |
---|
314 | if((pos = name_list_pos("useorbit", nl)) > -1 &&nl->inform[pos]) |
---|
315 | /* orbit specified */ |
---|
316 | { |
---|
317 | if (current_sequ->orbits == NULL) |
---|
318 | warning("orbit not found, ignored: ", pl->parameters[pos]->string); |
---|
319 | else |
---|
320 | { |
---|
321 | name = pl->parameters[pos]->string; |
---|
322 | if ((u_orb = name_list_pos(name, current_sequ->orbits->names)) < 0) |
---|
323 | warning("orbit not found, ignored: ", name); |
---|
324 | else set_option("useorbit", &k); |
---|
325 | } |
---|
326 | } |
---|
327 | pos = name_list_pos("keeporbit", nl); |
---|
328 | if(nl->inform[pos]) /* orbit specified tw_table*/ |
---|
329 | { |
---|
330 | name = pl->parameters[pos]->string; |
---|
331 | if (current_sequ->orbits == NULL) |
---|
332 | current_sequ->orbits = new_vector_list(10); |
---|
333 | else if (current_sequ->orbits->curr == current_sequ->orbits->max) |
---|
334 | grow_vector_list(current_sequ->orbits); |
---|
335 | if ((k_orb = name_list_pos(name, current_sequ->orbits->names)) < 0) |
---|
336 | { |
---|
337 | k_orb = add_to_name_list(permbuff(name), 0, |
---|
338 | current_sequ->orbits->names); |
---|
339 | current_sequ->orbits->vectors[k_orb] = new_double_array(6); |
---|
340 | } |
---|
341 | set_option("keeporbit", &k); |
---|
342 | } |
---|
343 | pos = name_list_pos("file", nl); |
---|
344 | if (nl->inform[pos]) |
---|
345 | { |
---|
346 | if ((filename = pl->parameters[pos]->string) == NULL) |
---|
347 | { |
---|
348 | if (pl->parameters[pos]->call_def != NULL) |
---|
349 | filename = pl->parameters[pos]->call_def->string; |
---|
350 | } |
---|
351 | if (filename == NULL) filename = permbuff("dummy"); |
---|
352 | w_file = 1; |
---|
353 | } |
---|
354 | else w_file = 0; |
---|
355 | tol_keep = get_variable("twiss_tol"); |
---|
356 | pos = name_list_pos("tolerance", nl); |
---|
357 | if (nl->inform[pos]) |
---|
358 | { |
---|
359 | tol = command_par_value("tolerance", current_twiss); |
---|
360 | set_variable("twiss_tol", &tol); |
---|
361 | } |
---|
362 | chrom_flg = command_par_value("chrom", current_twiss); |
---|
363 | |
---|
364 | /* |
---|
365 | end of command decoding |
---|
366 | */ |
---|
367 | |
---|
368 | zero_double(orbit0, 6); |
---|
369 | /* zero_double(disp0, 6); */ |
---|
370 | zero_double(oneturnmat, 36); |
---|
371 | |
---|
372 | /* Initialise Twiss parameters */ |
---|
373 | |
---|
374 | keep_twiss = current_twiss; |
---|
375 | |
---|
376 | if ((beta_def = twiss_input(current_twiss)) < 0) |
---|
377 | { |
---|
378 | if (beta_def == -1) warning("unknown beta0,", "Twiss ignored"); |
---|
379 | else if (beta_def == -2) |
---|
380 | warning("betx or bety missing,", "Twiss ignored"); |
---|
381 | set_variable("twiss_tol", &tol_keep); |
---|
382 | return; |
---|
383 | } |
---|
384 | set_option("twiss_inval", &beta_def); |
---|
385 | set_option("twiss_summ", &k); |
---|
386 | set_option("twiss_chrom", &chrom_flg); |
---|
387 | set_option("twiss_save", &k); |
---|
388 | |
---|
389 | /* Read Twiss parameters */ |
---|
390 | |
---|
391 | current_twiss = current_global_twiss; |
---|
392 | |
---|
393 | /*jt is the row number of previous element*/ |
---|
394 | |
---|
395 | if (jt <= 0) err = 1; |
---|
396 | if (err == 0) |
---|
397 | { |
---|
398 | err = double_from_table_row(table_name, "betx", &jt, &betx); |
---|
399 | err = double_from_table_row(table_name, "bety", &jt, &bety); |
---|
400 | err = double_from_table_row(table_name, "alfx", &jt, &alfx); |
---|
401 | err = double_from_table_row(table_name, "mux", &jt, &mux); |
---|
402 | /* mux = mux*twopi; frs 19.10.2006 */ |
---|
403 | err = double_from_table_row(table_name, "alfy", &jt, &alfy); |
---|
404 | err = double_from_table_row(table_name, "muy", &jt, &muy); |
---|
405 | /* muy = muy*twopi; frs 19.10.2006 */ |
---|
406 | err = double_from_table_row(table_name, "x", &jt, &x); |
---|
407 | err = double_from_table_row(table_name, "px", &jt, &px); |
---|
408 | err = double_from_table_row(table_name, "y", &jt, &y); |
---|
409 | err = double_from_table_row(table_name, "py", &jt, &py); |
---|
410 | err = double_from_table_row(table_name, "t", &jt, &t); |
---|
411 | err = double_from_table_row(table_name, "pt", &jt, &pt); |
---|
412 | err = double_from_table_row(table_name, "dx", &jt, &dx); |
---|
413 | err = double_from_table_row(table_name, "dpx", &jt, &dpx); |
---|
414 | err = double_from_table_row(table_name, "dy", &jt, &dy); |
---|
415 | err = double_from_table_row(table_name, "dpy", &jt, &dpy); |
---|
416 | err = double_from_table_row(table_name, "wx", &jt, &wx); |
---|
417 | err = double_from_table_row(table_name, "phix", &jt, &phix); |
---|
418 | err = double_from_table_row(table_name, "dmux", &jt, &dmux); |
---|
419 | err = double_from_table_row(table_name, "wy", &jt, &wy); |
---|
420 | err = double_from_table_row(table_name, "phiy", &jt, &phiy); |
---|
421 | err = double_from_table_row(table_name, "dmuy", &jt, &dmuy); |
---|
422 | err = double_from_table_row(table_name, "ddx", &jt, &ddx); |
---|
423 | err = double_from_table_row(table_name, "ddpx", &jt, &ddpx); |
---|
424 | err = double_from_table_row(table_name, "ddy", &jt, &ddy); |
---|
425 | err = double_from_table_row(table_name, "ddpy", &jt, &ddpy); |
---|
426 | err = double_from_table_row(table_name, "r11",&jt, &r11); |
---|
427 | err = double_from_table_row(table_name, "r12",&jt, &r12); |
---|
428 | err = double_from_table_row(table_name, "r21",&jt, &r21); |
---|
429 | err = double_from_table_row(table_name, "r22",&jt, &r22); |
---|
430 | err = double_from_table_row(table_name, "s",&jt, &s); |
---|
431 | |
---|
432 | /* Store these Twiss parameters as initial values */ |
---|
433 | |
---|
434 | current_twiss = keep_twiss; |
---|
435 | set_value("twiss", "betx" , &betx); |
---|
436 | nl->inform[name_list_pos("betx",nl)] = 1; |
---|
437 | set_value("twiss", "bety" , &bety); |
---|
438 | nl->inform[name_list_pos("bety",nl)] = 1; |
---|
439 | set_value("twiss", "alfx" , &alfx); |
---|
440 | nl->inform[name_list_pos("alfx",nl)] = 1; |
---|
441 | set_value("twiss", "mux", &mux); |
---|
442 | nl->inform[name_list_pos("mux",nl)] = 1; |
---|
443 | set_value("twiss", "alfy", &alfy); |
---|
444 | nl->inform[name_list_pos("alfy",nl)] = 1; |
---|
445 | set_value("twiss", "muy", &muy); |
---|
446 | nl->inform[name_list_pos("muy",nl)] = 1; |
---|
447 | set_value("twiss", "x", &x); |
---|
448 | nl->inform[name_list_pos("x",nl)] = 1; |
---|
449 | set_value("twiss", "px", &px); |
---|
450 | nl->inform[name_list_pos("px",nl)] = 1; |
---|
451 | set_value("twiss", "y", &y); |
---|
452 | nl->inform[name_list_pos("y",nl)] = 1; |
---|
453 | set_value("twiss", "py", &py); |
---|
454 | nl->inform[name_list_pos("py",nl)] = 1; |
---|
455 | set_value("twiss", "t", &t); |
---|
456 | nl->inform[name_list_pos("t",nl)] = 1; |
---|
457 | set_value("twiss", "pt", &pt); |
---|
458 | nl->inform[name_list_pos("pt",nl)] = 1; |
---|
459 | set_value("twiss", "dx", &dx); |
---|
460 | nl->inform[name_list_pos("dx",nl)] = 1; |
---|
461 | set_value("twiss", "dpx", &dpx); |
---|
462 | nl->inform[name_list_pos("dpx",nl)] = 1; |
---|
463 | set_value("twiss", "dy", &dy); |
---|
464 | nl->inform[name_list_pos("dy",nl)] = 1; |
---|
465 | set_value("twiss", "dpy", &dpy); |
---|
466 | nl->inform[name_list_pos("dpy",nl)] = 1; |
---|
467 | set_value("twiss", "wx", &wx); |
---|
468 | nl->inform[name_list_pos("wx",nl)] = 1; |
---|
469 | set_value("twiss", "phix", &phix); |
---|
470 | nl->inform[name_list_pos("phix",nl)] = 1; |
---|
471 | set_value("twiss", "dmux", &dmux); |
---|
472 | nl->inform[name_list_pos("dmux",nl)] = 1; |
---|
473 | set_value("twiss", "wy", &wy); |
---|
474 | nl->inform[name_list_pos("wy",nl)] = 1; |
---|
475 | set_value("twiss", "phiy", &phiy); |
---|
476 | nl->inform[name_list_pos("phiy",nl)] = 1; |
---|
477 | set_value("twiss", "dmuy", &dmuy); |
---|
478 | nl->inform[name_list_pos("dmuy",nl)] = 1; |
---|
479 | set_value("twiss", "ddx", &ddx); |
---|
480 | nl->inform[name_list_pos("ddx",nl)] = 1; |
---|
481 | set_value("twiss", "ddpx", &ddpx); |
---|
482 | nl->inform[name_list_pos("ddpx",nl)] = 1; |
---|
483 | set_value("twiss", "ddy", &ddy); |
---|
484 | nl->inform[name_list_pos("ddy",nl)] = 1; |
---|
485 | set_value("twiss", "ddpy", &ddpy); |
---|
486 | nl->inform[name_list_pos("ddpy",nl)] = 1; |
---|
487 | set_value("twiss", "r11", &r11); |
---|
488 | nl->inform[name_list_pos("r11",nl)] = 1; |
---|
489 | set_value("twiss", "r12", &r12); |
---|
490 | nl->inform[name_list_pos("r12",nl)] = 1; |
---|
491 | set_value("twiss", "r21", &r21); |
---|
492 | nl->inform[name_list_pos("r21",nl)] = 1; |
---|
493 | set_value("twiss", "r22", &r22); |
---|
494 | nl->inform[name_list_pos("r22",nl)] = 1; |
---|
495 | |
---|
496 | adjust_beam(); |
---|
497 | probe_beam = clone_command(current_beam); |
---|
498 | tmrefe_(oneturnmat); /* one-turn linear transfer map */ |
---|
499 | summ_table = make_table("summ", "summ", summ_table_cols, summ_table_types, |
---|
500 | twiss_deltas->curr+1); |
---|
501 | add_to_table_list(summ_table, table_register); |
---|
502 | l = strlen(table_embedded_name); |
---|
503 | tarr = new_int_array(l+1); |
---|
504 | conv_char(table_embedded_name, tarr); |
---|
505 | dummy_arr = new_int_array(5+1); |
---|
506 | conv_char("dummy",dummy_arr); |
---|
507 | if (get_option("twiss_sector")) |
---|
508 | { |
---|
509 | reset_sector(current_sequ, 0); |
---|
510 | set_sector(); |
---|
511 | } |
---|
512 | |
---|
513 | if (get_option("useorbit")) |
---|
514 | copy_double(current_sequ->orbits->vectors[u_orb]->a, orbit0, 6); |
---|
515 | else if (guess_flag) |
---|
516 | { |
---|
517 | for (i = 0; i < 6; i++) |
---|
518 | { |
---|
519 | if (guess_orbit[i] != zero) orbit0[i] = guess_orbit[i]; |
---|
520 | } |
---|
521 | } |
---|
522 | |
---|
523 | if(twiss_deltas->curr <= 0) |
---|
524 | fatal_error("PRO_TWISS_EMBEDDED "," - No twiss deltas"); |
---|
525 | |
---|
526 | for (i = 0; i < twiss_deltas->curr; i++) |
---|
527 | { |
---|
528 | twiss_table = make_table(table_embedded_name, "twiss", twiss_table_cols, |
---|
529 | twiss_table_types, current_sequ->n_nodes); |
---|
530 | |
---|
531 | twiss_table->dynamic = 1; /* flag for table row access to current row */ |
---|
532 | |
---|
533 | add_to_table_list(twiss_table, table_register); |
---|
534 | |
---|
535 | keep_table = current_sequ->tw_table; |
---|
536 | current_sequ->tw_table = twiss_table; |
---|
537 | |
---|
538 | twiss_table->org_sequ = current_sequ; |
---|
539 | adjust_probe(twiss_deltas->a[i]); /* sets correct gamma, beta, etc. */ |
---|
540 | |
---|
541 | adjust_rfc(); /* sets freq in rf-cavities from probe */ |
---|
542 | current_node = current_sequ->range_start; |
---|
543 | set_option("twiss_inval", &inval); |
---|
544 | |
---|
545 | // CALL TWISS |
---|
546 | twiss_(oneturnmat, disp0, tarr->i, dummy_arr->i); /* different call */ |
---|
547 | |
---|
548 | if ((twiss_success = get_option("twiss_success"))) |
---|
549 | { |
---|
550 | if (get_option("keeporbit")) copy_double(orbit0, |
---|
551 | current_sequ->orbits->vectors[k_orb]->a, 6); |
---|
552 | fill_twiss_header(twiss_table); |
---|
553 | if (i == 0) exec_savebeta(); /* fill beta0 at first delta_p only */ |
---|
554 | if (w_file) out_table(table_embedded_name, twiss_table, filename); |
---|
555 | } |
---|
556 | else warning("Twiss failed: ", "MAD-X continues"); |
---|
557 | } |
---|
558 | |
---|
559 | if (sec_file) |
---|
560 | { |
---|
561 | fclose(sec_file); sec_file = NULL; |
---|
562 | } |
---|
563 | tarr = delete_int_array(tarr); |
---|
564 | dummy_arr = delete_int_array(dummy_arr); |
---|
565 | if (twiss_success && get_option("twiss_print")) print_table(summ_table); |
---|
566 | } |
---|
567 | else warning("Embedded Twiss failed: ", "MAD-X continues"); |
---|
568 | |
---|
569 | /* cleanup */ |
---|
570 | current_beam = keep_beam; |
---|
571 | probe_beam = delete_command(probe_beam); |
---|
572 | set_option("twiss_print", &k); |
---|
573 | k = 0; |
---|
574 | set_option("couple", &k); |
---|
575 | set_option("chrom", &k); |
---|
576 | set_option("rmatrix", &k); |
---|
577 | /* set_option("centre", &k); */ |
---|
578 | set_option("twiss_sector", &k); |
---|
579 | set_option("keeporbit", &k); |
---|
580 | set_option("useorbit", &k); |
---|
581 | set_option("info", &keep_info); |
---|
582 | set_variable("twiss_tol", &tol_keep); |
---|
583 | current_sequ->tw_table = keep_table; |
---|
584 | |
---|
585 | /* Reset embedded_flag */ |
---|
586 | |
---|
587 | embedded_flag = 0; |
---|
588 | } |
---|
589 | |
---|
590 | static void |
---|
591 | set_twiss_deltas(struct command* comm) |
---|
592 | { |
---|
593 | char* string; |
---|
594 | int i, k = 0, n = 0, pos; |
---|
595 | double s, sign = one, ar[3]; |
---|
596 | struct name_list* nl = comm->par_names; |
---|
597 | pos = name_list_pos("deltap", nl); |
---|
598 | twiss_deltas->curr = 1; |
---|
599 | twiss_deltas->a[0] = zero; |
---|
600 | if ((pos = name_list_pos("deltap", nl)) >= 0 && nl->inform[pos] |
---|
601 | && (string = comm->par->parameters[pos]->string) != NULL) |
---|
602 | { |
---|
603 | pre_split(string, c_dum, 0); |
---|
604 | mysplit(c_dum->c, tmp_p_array); |
---|
605 | while (k < tmp_p_array->curr) |
---|
606 | { |
---|
607 | for (i = k; i < tmp_p_array->curr; i++) |
---|
608 | if (*tmp_p_array->p[i] == ':') break; |
---|
609 | ar[n++] = double_from_expr(tmp_p_array->p, k, i-1); |
---|
610 | k = i + 1; |
---|
611 | } |
---|
612 | if (n == 1) twiss_deltas->a[0] = ar[0]; |
---|
613 | else /* there is a range given - fill array */ |
---|
614 | { |
---|
615 | if (n == 2) ar[n++] = ar[1] - ar[0]; |
---|
616 | if (ar[2] == zero) twiss_deltas->a[0] = ar[0]; |
---|
617 | else if (ar[2] * (ar[1] - ar[0]) < zero) |
---|
618 | warning("illegal deltap range ignored:", string); |
---|
619 | else |
---|
620 | { |
---|
621 | twiss_deltas->a[0] = ar[0]; |
---|
622 | if (ar[2] < zero) sign = -sign; |
---|
623 | for (s = sign * (ar[0] + ar[2]); |
---|
624 | s <= sign * ar[1]; s+= sign * ar[2]) |
---|
625 | { |
---|
626 | if (twiss_deltas->curr == twiss_deltas->max) |
---|
627 | { |
---|
628 | sprintf(c_dum->c, "%d values", twiss_deltas->max); |
---|
629 | warning("deltap loop cut at", c_dum->c); |
---|
630 | break; |
---|
631 | } |
---|
632 | twiss_deltas->a[twiss_deltas->curr] |
---|
633 | = twiss_deltas->a[twiss_deltas->curr-1] + ar[2]; |
---|
634 | twiss_deltas->curr++; |
---|
635 | } |
---|
636 | } |
---|
637 | } |
---|
638 | } |
---|
639 | } |
---|
640 | |
---|
641 | // public interface |
---|
642 | |
---|
643 | void |
---|
644 | copy_twiss_data(double* twiss_data) |
---|
645 | { |
---|
646 | copy_double(twiss_data, current_node->match_data, 74); |
---|
647 | } |
---|
648 | |
---|
649 | void |
---|
650 | get_twiss_data(double* twiss_data) |
---|
651 | { |
---|
652 | copy_double(current_node->match_data, twiss_data, 74); |
---|
653 | } |
---|
654 | |
---|
655 | void |
---|
656 | get_disp0(double* disp) |
---|
657 | { |
---|
658 | copy_double(disp0, disp, 6); |
---|
659 | } |
---|
660 | |
---|
661 | void |
---|
662 | complete_twiss_table(struct table* t) |
---|
663 | /* fills all items missing after "twiss" into twiss table */ |
---|
664 | { |
---|
665 | int i, j, mult, n, myrbend; |
---|
666 | double el, val; |
---|
667 | struct node* c_node; |
---|
668 | char tmp[16]; |
---|
669 | |
---|
670 | if (t == NULL) return; |
---|
671 | i = t->curr; |
---|
672 | c_node = current_node; |
---|
673 | mult = strcmp(c_node->base_name, "multipole") == 0 ? 1 : 0; |
---|
674 | t->s_cols[0][i] = tmpbuff(c_node->name); |
---|
675 | t->s_cols[1][i] = tmpbuff(c_node->base_name); |
---|
676 | t->s_cols[twiss_fill_end+1][i] = tmpbuff(c_node->p_elem->parent->name); |
---|
677 | for (j = twiss_opt_end+1; j<= twiss_fill_end; j++) |
---|
678 | { |
---|
679 | el = c_node->length; |
---|
680 | strcpy(tmp, twiss_table_cols[j]); |
---|
681 | myrbend = (strcmp(c_node->p_elem->base_type->name, "rbend") == 0); |
---|
682 | if (strcmp(twiss_table_cols[j], "l") == 0) val = el; |
---|
683 | else if (strcmp(tmp, "slot_id") == 0) val = el_par_value(tmp, c_node->p_elem); |
---|
684 | else if (strcmp(tmp, "e1") == 0 || strcmp(tmp, "e2") == 0) |
---|
685 | { |
---|
686 | if(myrbend) |
---|
687 | { |
---|
688 | val = el_par_value(tmp, c_node->p_elem) + |
---|
689 | c_node->other_bv * el_par_value("angle", c_node->p_elem) / two; |
---|
690 | } |
---|
691 | else |
---|
692 | { |
---|
693 | val = el_par_value(tmp, c_node->p_elem); |
---|
694 | } |
---|
695 | } |
---|
696 | else if (strcmp(tmp, "assembly_id") == 0) val = el_par_value(tmp, c_node->p_elem); |
---|
697 | else if (strcmp(tmp, "mech_sep") == 0) val = el_par_value(tmp, c_node->p_elem); |
---|
698 | /*== jln 11.11.2010 dealt with the new property v_pos as for mech_sep */ |
---|
699 | else if (strcmp(tmp, "v_pos") == 0 ) val = el_par_value(tmp, c_node->p_elem); |
---|
700 | /*==*/ |
---|
701 | else if (strcmp(tmp, "lrad") == 0) val = el_par_value(tmp, c_node->p_elem); |
---|
702 | else if (strcmp(tmp, "h1") == 0 && |
---|
703 | strcmp(c_node->base_name, "dipedge") == 0) val = el_par_value("h", c_node->p_elem); |
---|
704 | else if(mult) |
---|
705 | { |
---|
706 | if(j<=twiss_mult_end) |
---|
707 | { |
---|
708 | val = mult_par(twiss_table_cols[j], c_node->p_elem); |
---|
709 | val *= c_node->other_bv; /* dipole_bv kill initiative SF TR FS */ |
---|
710 | } |
---|
711 | else |
---|
712 | { |
---|
713 | val = el_par_value(tmp, c_node->p_elem); |
---|
714 | } |
---|
715 | } |
---|
716 | else |
---|
717 | { |
---|
718 | strcpy(tmp, twiss_table_cols[j]); |
---|
719 | n = strlen(tmp) - 1; |
---|
720 | if (n > 1 && tmp[0] == 'k' && isdigit(tmp[1]) && tmp[n] == 'l') |
---|
721 | tmp[n] = '\0'; /* suppress trailing l in k0l etc. */ |
---|
722 | if (el != zero && n > 1 && tmp[0] == 'k' && tmp[1] == 's' && tmp[n] == 'i') |
---|
723 | tmp[n] = '\0'; /* suppress trailing i in ksi */ |
---|
724 | val = el_par_value(tmp, c_node->p_elem); |
---|
725 | if (n > 1 && tmp[0] == 'k' && isdigit(tmp[1])) |
---|
726 | val *= c_node->other_bv; /* dipole_bv kill initiative SF TR FS */ |
---|
727 | else if (strstr(tmp, "kick") || strcmp(tmp, "angle") == 0 || |
---|
728 | strcmp(tmp, "ks") == 0 || strcmp(tmp, "ksi") == 0 || |
---|
729 | strcmp(tmp, "volt") == 0 ) |
---|
730 | val *= c_node->other_bv; /* dipole_bv kill initiative SF TR FS */ |
---|
731 | if (el != zero) |
---|
732 | { |
---|
733 | if (strstr(tmp,"kick") == NULL && strcmp(tmp, "angle") |
---|
734 | && strcmp(tmp, "tilt") && strcmp(tmp, "e1") && strcmp(tmp, "e2") |
---|
735 | && strcmp(tmp, "h1") && strcmp(tmp, "h2") && strcmp(tmp, "hgap") |
---|
736 | && strcmp(tmp, "fint") && strcmp(tmp, "fintx") |
---|
737 | && strcmp(tmp, "volt") && strcmp(tmp, "lag") |
---|
738 | && strcmp(tmp, "freq") && strcmp(tmp, "harmon")) val *= el; |
---|
739 | } |
---|
740 | } |
---|
741 | t->d_cols[j][i] = val; |
---|
742 | } |
---|
743 | } |
---|
744 | |
---|
745 | void |
---|
746 | pro_twiss(void) |
---|
747 | /* controls twiss module */ |
---|
748 | { |
---|
749 | struct command* keep_beam = current_beam; |
---|
750 | struct name_list* nl; |
---|
751 | struct command_parameter_list* pl; |
---|
752 | struct int_array* tarr; |
---|
753 | struct int_array* tarr_sector; |
---|
754 | struct node *nodes[2], *use_range[2]; |
---|
755 | char *filename = NULL, *name, *table_name, *sector_name = NULL; |
---|
756 | char dummy[NAME_L] = "dummy", *sector_table_name = dummy; /* second string required by twiss() */ |
---|
757 | /* will be set to a proper string in case twiss_sector option selected */ |
---|
758 | double tol,tol_keep, q1_val_p = 0, q2_val_p = 0, q1_val, q2_val, dq1, dq2; |
---|
759 | int i, j, l, lp, k_orb = 0, u_orb = 0, pos, k_save = 1, k = 1, k_sect, |
---|
760 | w_file, beta_def; |
---|
761 | int chrom_flg; |
---|
762 | int keep_info = get_option("info"); |
---|
763 | i = keep_info * get_option("twiss_print"); |
---|
764 | set_option("info", &i); |
---|
765 | |
---|
766 | if (current_twiss == NULL) |
---|
767 | { |
---|
768 | seterrorflag(2,"pro_twiss","No twiss command seen yet"); |
---|
769 | warning("pro_twiss","No twiss command seen yet!"); |
---|
770 | return; |
---|
771 | } |
---|
772 | |
---|
773 | if (current_twiss->par_names == NULL) |
---|
774 | { |
---|
775 | seterrorflag(3,"pro_twiss","Last twiss has NULL par_names pointer. Cannot proceed further."); |
---|
776 | warning("pro_twiss","Last twiss has NULL par_names pointer. Cannot proceed further."); |
---|
777 | return; |
---|
778 | } |
---|
779 | |
---|
780 | if (current_twiss->par == NULL) |
---|
781 | { |
---|
782 | seterrorflag(4,"pro_twiss","Last twiss has NULL par pointer. Cannot proceed further."); |
---|
783 | warning("pro_twiss","Last twiss has NULL par pointer. Cannot proceed further."); |
---|
784 | return; |
---|
785 | } |
---|
786 | |
---|
787 | |
---|
788 | nl = current_twiss->par_names; |
---|
789 | pl = current_twiss->par; |
---|
790 | |
---|
791 | if (match_is_on) k_save = 0; /* match gets its own variable transfer - |
---|
792 | this can be overridden with option "slow" |
---|
793 | on match command */ |
---|
794 | |
---|
795 | /* |
---|
796 | start command decoding |
---|
797 | */ |
---|
798 | pos = name_list_pos("sequence", nl); |
---|
799 | if(nl->inform[pos]) /* sequence specified */ |
---|
800 | { |
---|
801 | name = pl->parameters[pos]->string; |
---|
802 | if ((lp = name_list_pos(name, sequences->list)) > -1) |
---|
803 | current_sequ = sequences->sequs[lp]; |
---|
804 | else |
---|
805 | { |
---|
806 | warning("unknown sequence ignored:", name); |
---|
807 | return; |
---|
808 | } |
---|
809 | } |
---|
810 | |
---|
811 | if (current_sequ == NULL || current_sequ->ex_start == NULL) |
---|
812 | { |
---|
813 | warning("sequence not active,", "Twiss ignored"); |
---|
814 | return; |
---|
815 | } |
---|
816 | if(get_option("twiss_print")) fprintf(prt_file, "enter Twiss module\n"); |
---|
817 | if (attach_beam(current_sequ) == 0) |
---|
818 | fatal_error("TWISS - sequence without beam:", current_sequ->name); |
---|
819 | |
---|
820 | pos = name_list_pos("table", nl); |
---|
821 | if(nl->inform[pos]) /* table name specified - overrides save */ |
---|
822 | { |
---|
823 | if ((table_name = pl->parameters[pos]->string) == NULL) |
---|
824 | table_name = pl->parameters[pos]->call_def->string; |
---|
825 | } |
---|
826 | else if((pos = name_list_pos("save", nl)) > -1 && nl->inform[pos]) /* save name specified */ |
---|
827 | { |
---|
828 | k_save = 1; |
---|
829 | if ((table_name = pl->parameters[pos]->string) == NULL) |
---|
830 | table_name = pl->parameters[pos]->call_def->string; |
---|
831 | } |
---|
832 | else table_name = "twiss"; |
---|
833 | |
---|
834 | if ((k_sect = get_value(current_command->name,"sectormap")) != 0) |
---|
835 | { |
---|
836 | set_option("twiss_sector", &k_sect); |
---|
837 | /* sector_table - start */ |
---|
838 | pos = name_list_pos("sectortable",nl); |
---|
839 | if (nl->inform[pos]) { |
---|
840 | if ((sector_table_name = pl->parameters[pos]->string) == NULL) |
---|
841 | sector_table_name = pl->parameters[pos]->call_def->string; |
---|
842 | } |
---|
843 | else { |
---|
844 | sector_table_name = pl->parameters[pos]->call_def->string; |
---|
845 | } |
---|
846 | /* sector_table - end */ |
---|
847 | pos = name_list_pos("sectorfile", nl); |
---|
848 | if(nl->inform[pos]) |
---|
849 | { |
---|
850 | if ((sector_name = pl->parameters[pos]->string) == NULL) |
---|
851 | sector_name = pl->parameters[pos]->call_def->string; |
---|
852 | } |
---|
853 | else sector_name = pl->parameters[pos]->call_def->string; /* filename for sector file */ |
---|
854 | |
---|
855 | } |
---|
856 | use_range[0] = current_sequ->range_start; |
---|
857 | use_range[1] = current_sequ->range_end; |
---|
858 | if ((pos = name_list_pos("range", nl)) > -1 && nl->inform[pos]) |
---|
859 | { |
---|
860 | if (get_sub_range(pl->parameters[pos]->string, current_sequ, nodes)) |
---|
861 | { |
---|
862 | current_sequ->range_start = nodes[0]; |
---|
863 | current_sequ->range_end = nodes[1]; |
---|
864 | } |
---|
865 | else warning("illegal range ignored:", pl->parameters[pos]->string); |
---|
866 | } |
---|
867 | for (j = 0; j < current_sequ->n_nodes; j++) |
---|
868 | { |
---|
869 | if (current_sequ->all_nodes[j] == current_sequ->range_start) break; |
---|
870 | } |
---|
871 | if((pos = name_list_pos("useorbit", nl)) > -1 &&nl->inform[pos]) |
---|
872 | /* orbit specified */ |
---|
873 | { |
---|
874 | if (current_sequ->orbits == NULL) |
---|
875 | warning("orbit not found, ignored: ", pl->parameters[pos]->string); |
---|
876 | else |
---|
877 | { |
---|
878 | name = pl->parameters[pos]->string; |
---|
879 | if ((u_orb = name_list_pos(name, current_sequ->orbits->names)) < 0) |
---|
880 | warning("orbit not found, ignored: ", name); |
---|
881 | else set_option("useorbit", &k); |
---|
882 | } |
---|
883 | } |
---|
884 | pos = name_list_pos("centre", nl); |
---|
885 | if(nl->inform[pos]) |
---|
886 | set_option("centre", &k); |
---|
887 | else |
---|
888 | { |
---|
889 | k = 0; |
---|
890 | set_option("centre", &k); |
---|
891 | k = 1; |
---|
892 | } |
---|
893 | pos = name_list_pos("keeporbit", nl); |
---|
894 | if(nl->inform[pos]) /* orbit specified */ |
---|
895 | { |
---|
896 | name = pl->parameters[pos]->string; |
---|
897 | if (current_sequ->orbits == NULL) |
---|
898 | current_sequ->orbits = new_vector_list(10); |
---|
899 | else if (current_sequ->orbits->curr == current_sequ->orbits->max) |
---|
900 | grow_vector_list(current_sequ->orbits); |
---|
901 | if ((k_orb = name_list_pos(name, current_sequ->orbits->names)) < 0) |
---|
902 | { |
---|
903 | k_orb = add_to_name_list(permbuff(name), 0, |
---|
904 | current_sequ->orbits->names); |
---|
905 | current_sequ->orbits->vectors[k_orb] = new_double_array(6); |
---|
906 | } |
---|
907 | set_option("keeporbit", &k); |
---|
908 | } |
---|
909 | pos = name_list_pos("file", nl); |
---|
910 | if (nl->inform[pos]) |
---|
911 | { |
---|
912 | if ((filename = pl->parameters[pos]->string) == NULL) |
---|
913 | { |
---|
914 | if (pl->parameters[pos]->call_def != NULL) |
---|
915 | filename = pl->parameters[pos]->call_def->string; |
---|
916 | } |
---|
917 | if (filename == NULL) filename = permbuff("dummy"); |
---|
918 | w_file = 1; |
---|
919 | |
---|
920 | strcpy(aptwfile,filename); /* IW 02.12.2004 */ |
---|
921 | |
---|
922 | } |
---|
923 | else w_file = 0; |
---|
924 | tol_keep = get_variable("twiss_tol"); |
---|
925 | pos = name_list_pos("tolerance", nl); |
---|
926 | if (nl->inform[pos]) |
---|
927 | { |
---|
928 | tol = command_par_value("tolerance", current_twiss); |
---|
929 | set_variable("twiss_tol", &tol); |
---|
930 | } |
---|
931 | pos = name_list_pos("chrom", nl); |
---|
932 | chrom_flg = command_par_value("chrom", current_twiss); |
---|
933 | if((pos = name_list_pos("notable", nl)) > -1 &&nl->inform[pos]) k_save = 0; |
---|
934 | /* |
---|
935 | end of command decoding |
---|
936 | */ |
---|
937 | |
---|
938 | zero_double(orbit0, 6); |
---|
939 | /* zero_double(disp0, 6); */ |
---|
940 | zero_double(oneturnmat, 36); |
---|
941 | if ((beta_def = twiss_input(current_twiss)) < 0) |
---|
942 | { |
---|
943 | if (beta_def == -1) warning("unknown beta0,", "Twiss ignored"); |
---|
944 | else if (beta_def == -2) |
---|
945 | warning("betx or bety missing,", "Twiss ignored"); |
---|
946 | set_variable("twiss_tol", &tol_keep); |
---|
947 | return; |
---|
948 | } |
---|
949 | set_option("twiss_inval", &beta_def); |
---|
950 | set_option("twiss_summ", &k); |
---|
951 | set_option("twiss_chrom", &chrom_flg); |
---|
952 | set_option("twiss_save", &k_save); |
---|
953 | set_twiss_deltas(current_twiss); |
---|
954 | adjust_beam(); |
---|
955 | probe_beam = clone_command(current_beam); |
---|
956 | tmrefe_(oneturnmat); /* one-turn linear transfer map */ |
---|
957 | summ_table = make_table("summ", "summ", summ_table_cols, summ_table_types, |
---|
958 | twiss_deltas->curr+1); |
---|
959 | add_to_table_list(summ_table, table_register); |
---|
960 | l = strlen(table_name); |
---|
961 | tarr = new_int_array(l+1); |
---|
962 | conv_char(table_name, tarr); |
---|
963 | /* now create the sector table */ |
---|
964 | l = strlen(sector_table_name); |
---|
965 | tarr_sector = new_int_array(l+1); |
---|
966 | conv_char(sector_table_name, tarr_sector); |
---|
967 | |
---|
968 | if (get_option("twiss_sector")) |
---|
969 | { |
---|
970 | reset_sector(current_sequ, 0); |
---|
971 | set_sector(); |
---|
972 | |
---|
973 | twiss_sector_table = make_table(sector_table_name, "sectormap", |
---|
974 | twiss_sector_table_cols, |
---|
975 | twiss_sector_table_types, |
---|
976 | 1); /* start with one row at first */ |
---|
977 | twiss_sector_table->dynamic = 1; /* flag for access to current row */ |
---|
978 | add_to_table_list(twiss_sector_table, table_register); |
---|
979 | } |
---|
980 | if (get_option("useorbit")) |
---|
981 | copy_double(current_sequ->orbits->vectors[u_orb]->a, orbit0, 6); |
---|
982 | else if (guess_flag) |
---|
983 | { |
---|
984 | for (i = 0; i < 6; i++) |
---|
985 | { |
---|
986 | if (guess_orbit[i] != zero) orbit0[i] = guess_orbit[i]; |
---|
987 | } |
---|
988 | } |
---|
989 | |
---|
990 | for (i = 0; i < twiss_deltas->curr; i++) |
---|
991 | { |
---|
992 | if (chrom_flg) { /* calculate chromaticity from tune difference - HG 6.2.09*/ |
---|
993 | twiss_table = make_table(table_name, "twiss", twiss_table_cols, twiss_table_types, current_sequ->n_nodes); |
---|
994 | twiss_table->dynamic = 1; /* flag for table row access to current row */ |
---|
995 | add_to_table_list(twiss_table, table_register); |
---|
996 | current_sequ->tw_table = twiss_table; |
---|
997 | current_sequ->tw_valid = 1; |
---|
998 | twiss_table->org_sequ = current_sequ; |
---|
999 | adjust_probe(twiss_deltas->a[i]+DQ_DELTAP); |
---|
1000 | adjust_rfc(); /* sets freq in rf-cavities from probe */ |
---|
1001 | current_node = current_sequ->ex_start; |
---|
1002 | |
---|
1003 | // CALL TWISS |
---|
1004 | twiss_(oneturnmat, disp0, tarr->i,tarr_sector->i); |
---|
1005 | pos = name_list_pos("q1", summ_table->columns); |
---|
1006 | q1_val_p = summ_table->d_cols[pos][i]; |
---|
1007 | pos = name_list_pos("q2", summ_table->columns); |
---|
1008 | q2_val_p = summ_table->d_cols[pos][i]; |
---|
1009 | } |
---|
1010 | if (k_save) { |
---|
1011 | twiss_table = make_table(table_name, "twiss", twiss_table_cols, twiss_table_types, current_sequ->n_nodes); |
---|
1012 | twiss_table->dynamic = 1; /* flag for table row access to current row */ |
---|
1013 | add_to_table_list(twiss_table, table_register); |
---|
1014 | current_sequ->tw_table = twiss_table; |
---|
1015 | current_sequ->tw_valid = 1; |
---|
1016 | twiss_table->org_sequ = current_sequ; |
---|
1017 | } |
---|
1018 | adjust_probe(twiss_deltas->a[i]); /* sets correct gamma, beta, etc. */ |
---|
1019 | adjust_rfc(); /* sets freq in rf-cavities from probe */ |
---|
1020 | current_node = current_sequ->ex_start; |
---|
1021 | |
---|
1022 | // CALL TWISS |
---|
1023 | twiss_(oneturnmat, disp0, tarr->i,tarr_sector->i); |
---|
1024 | augment_count_("summ "); |
---|
1025 | if ((twiss_success = get_option("twiss_success"))) |
---|
1026 | { |
---|
1027 | if (chrom_flg) /* calculate chromaticity from tune difference - HG 6.2.09*/ |
---|
1028 | { |
---|
1029 | pos = name_list_pos("q1", summ_table->columns); |
---|
1030 | q1_val = summ_table->d_cols[pos][i]; |
---|
1031 | pos = name_list_pos("q2", summ_table->columns); |
---|
1032 | q2_val = summ_table->d_cols[pos][i]; |
---|
1033 | dq1 = (q1_val_p - q1_val) / DQ_DELTAP; |
---|
1034 | dq2 = (q2_val_p - q2_val) / DQ_DELTAP; |
---|
1035 | pos = name_list_pos("dq1", summ_table->columns); |
---|
1036 | summ_table->d_cols[pos][i] = dq1; |
---|
1037 | pos = name_list_pos("dq2", summ_table->columns); |
---|
1038 | summ_table->d_cols[pos][i] = dq2; |
---|
1039 | } |
---|
1040 | if (get_option("keeporbit")) copy_double(orbit0, |
---|
1041 | current_sequ->orbits->vectors[k_orb]->a, 6); |
---|
1042 | if (k_save) fill_twiss_header(twiss_table); |
---|
1043 | if (i == 0) exec_savebeta(); /* fill beta0 at first delta_p only */ |
---|
1044 | if (k_save && w_file) out_table(table_name, twiss_table, filename); |
---|
1045 | } |
---|
1046 | else |
---|
1047 | { |
---|
1048 | seterrorflag(1,"pro_twiss","TWISS failed"); |
---|
1049 | warning("Twiss failed: ", "MAD-X continues"); |
---|
1050 | } |
---|
1051 | } |
---|
1052 | if (get_option("twiss_sector")){ |
---|
1053 | out_table( sector_table_name, twiss_sector_table, sector_name ); |
---|
1054 | } |
---|
1055 | |
---|
1056 | tarr = delete_int_array(tarr); |
---|
1057 | tarr_sector = delete_int_array(tarr_sector); |
---|
1058 | |
---|
1059 | if (twiss_success && get_option("twiss_print")) print_table(summ_table); |
---|
1060 | |
---|
1061 | /* cleanup */ |
---|
1062 | current_beam = keep_beam; |
---|
1063 | probe_beam = delete_command(probe_beam); |
---|
1064 | k = 0; |
---|
1065 | set_option("couple", &k); |
---|
1066 | set_option("chrom", &k); |
---|
1067 | set_option("rmatrix", &k); |
---|
1068 | set_option("twiss_sector", &k); |
---|
1069 | set_option("keeporbit", &k); |
---|
1070 | set_option("useorbit", &k); |
---|
1071 | set_option("info", &keep_info); |
---|
1072 | set_variable("twiss_tol", &tol_keep); |
---|
1073 | current_sequ->range_start = use_range[0]; |
---|
1074 | current_sequ->range_end = use_range[1]; |
---|
1075 | } |
---|
1076 | |
---|
1077 | int |
---|
1078 | embedded_twiss(void) |
---|
1079 | /* controls twiss module to create a twiss table for interpolated nodes |
---|
1080 | between two elements */ |
---|
1081 | |
---|
1082 | { |
---|
1083 | struct name_list* tnl; /* OB 31.1.2002: local name list for TWISS input definition */ |
---|
1084 | struct in_cmd* cmd; |
---|
1085 | struct command* current_global_twiss; |
---|
1086 | struct command_parameter* cp; |
---|
1087 | struct name_list* nl; |
---|
1088 | // struct command_parameter_list* pl; // not used |
---|
1089 | char* embedded_twiss_beta[2]; |
---|
1090 | int j, pos, tpos; |
---|
1091 | int izero = 0; |
---|
1092 | |
---|
1093 | cmd = embedded_twiss_cmd; |
---|
1094 | nl = cmd->clone->par_names; |
---|
1095 | // pl = cmd->clone->par; // not used |
---|
1096 | keep_tw_print = get_option("twiss_print"); |
---|
1097 | set_option("twiss_print", &izero); |
---|
1098 | |
---|
1099 | /* START defining a TWISS input command for default sequence */ |
---|
1100 | |
---|
1101 | local_twiss[0] = new_in_cmd(10); |
---|
1102 | local_twiss[0]->type = 0; |
---|
1103 | local_twiss[0]->clone = local_twiss[0]->cmd_def |
---|
1104 | = clone_command(find_command("twiss", defined_commands)); |
---|
1105 | tnl = local_twiss[0]->cmd_def->par_names; |
---|
1106 | tpos = name_list_pos("sequence", tnl); |
---|
1107 | local_twiss[0]->cmd_def->par->parameters[tpos]->string = current_sequ->name; |
---|
1108 | local_twiss[0]->cmd_def->par_names->inform[tpos] = 1; |
---|
1109 | |
---|
1110 | /* END defining a TWISS input command for default sequence */ |
---|
1111 | |
---|
1112 | if (current_sequ == NULL || current_sequ->ex_start == NULL) |
---|
1113 | { |
---|
1114 | warning("Command called without active sequence,", "ignored"); |
---|
1115 | return 1; |
---|
1116 | } |
---|
1117 | /* END CHK-SEQ; OB 1.2.2002 */ |
---|
1118 | |
---|
1119 | for (j = 0; j < local_twiss[0]->cmd_def->par->curr; j++) |
---|
1120 | { |
---|
1121 | tnl = local_twiss[0]->cmd_def->par_names; |
---|
1122 | tpos = name_list_pos("sequence", tnl); |
---|
1123 | if (j != tpos) local_twiss[0]->cmd_def->par_names->inform[j] = 0; |
---|
1124 | } |
---|
1125 | |
---|
1126 | /* START CHK-BETA-INPUT; OB 1.2.2002 */ |
---|
1127 | /* START CHK-BETA0; OB 23.1.2002 */ |
---|
1128 | pos = name_list_pos("beta0", nl); |
---|
1129 | if (pos > -1 && nl->inform[pos]) /* parameter has been read */ |
---|
1130 | { |
---|
1131 | /* beta0 specified */ |
---|
1132 | cp = cmd->clone->par->parameters[pos]; |
---|
1133 | embedded_twiss_beta[0] = buffer(cp->m_string->p[0]); |
---|
1134 | |
---|
1135 | /* START defining a TWISS input command for the sequence */ |
---|
1136 | tnl = local_twiss[0]->cmd_def->par_names; |
---|
1137 | tpos = name_list_pos("beta0", tnl); |
---|
1138 | local_twiss[0]->cmd_def->par_names->inform[tpos] = 1; |
---|
1139 | local_twiss[0]->cmd_def->par->parameters[tpos]->string = embedded_twiss_beta[0]; |
---|
1140 | /* END defining a TWISS input command for the sequence */ |
---|
1141 | } |
---|
1142 | |
---|
1143 | /* END CHK-BETA0; OB 23.1.2002 */ |
---|
1144 | |
---|
1145 | /* END CHK-RANGE; OB 12.11.2002 */ |
---|
1146 | |
---|
1147 | /* START CHK-USEORBIT; HG 28.1.2003 */ |
---|
1148 | pos = name_list_pos("useorbit", nl); |
---|
1149 | if (pos > -1 && nl->inform[pos]) /* parameter has been read */ |
---|
1150 | { |
---|
1151 | /* useorbit specified */ |
---|
1152 | cp = cmd->clone->par->parameters[pos]; |
---|
1153 | /* START adding useorbit to TWISS input command for each sequence */ |
---|
1154 | tnl = local_twiss[0]->cmd_def->par_names; |
---|
1155 | tpos = name_list_pos("useorbit", tnl); |
---|
1156 | local_twiss[0]->cmd_def->par_names->inform[tpos] = 1; |
---|
1157 | local_twiss[0]->cmd_def->par->parameters[tpos]->string |
---|
1158 | = buffer(cp->m_string->p[0]); |
---|
1159 | /* END adding range to TWISS input command for each sequence */ |
---|
1160 | } |
---|
1161 | /* END CHK-USEORBIT; HG 28.1.2003 */ |
---|
1162 | |
---|
1163 | /* START CHK-KEEPORBIT; HG 28.1.2003 */ |
---|
1164 | pos = name_list_pos("keeporbit", nl); |
---|
1165 | if (pos > -1 && nl->inform[pos]) /* parameter has been read */ |
---|
1166 | { |
---|
1167 | /* keeporbit specified */ |
---|
1168 | cp = cmd->clone->par->parameters[pos]; |
---|
1169 | /* START adding keeporbit to TWISS input command for each sequence */ |
---|
1170 | tnl = local_twiss[0]->cmd_def->par_names; |
---|
1171 | tpos = name_list_pos("keeporbit", tnl); |
---|
1172 | local_twiss[0]->cmd_def->par_names->inform[tpos] = 1; |
---|
1173 | local_twiss[0]->cmd_def->par->parameters[tpos]->string |
---|
1174 | = buffer(cp->m_string->p[0]); |
---|
1175 | /* END adding range to TWISS input command for each sequence */ |
---|
1176 | } |
---|
1177 | /* END CHK-KEEPORBIT; HG 28.1.2003 */ |
---|
1178 | |
---|
1179 | /* END CHK-BETA-INPUT; OB 1.2.2002 */ |
---|
1180 | |
---|
1181 | /* START generating a TWISS table via 'pro_twiss'; OB 1.2.2002 */ |
---|
1182 | |
---|
1183 | current_global_twiss = current_twiss; |
---|
1184 | current_twiss = local_twiss[0]->clone; |
---|
1185 | pro_embedded_twiss(current_global_twiss); |
---|
1186 | |
---|
1187 | /* END generating a TWISS table via 'pro_twiss' */ |
---|
1188 | current_twiss = current_global_twiss; |
---|
1189 | |
---|
1190 | return 0; |
---|
1191 | } |
---|
1192 | |
---|
1193 | void |
---|
1194 | store_beta0(struct in_cmd* cmd) |
---|
1195 | { |
---|
1196 | int k = cmd->decl_start - 1; |
---|
1197 | if (k == 0) warning("beta0 without label:", "ignored"); |
---|
1198 | else |
---|
1199 | { |
---|
1200 | cmd->clone_flag = 1; /* do not delete */ |
---|
1201 | add_to_command_list(cmd->tok_list->p[0], cmd->clone, beta0_list, 0); |
---|
1202 | } |
---|
1203 | } |
---|
1204 | |
---|
1205 | void |
---|
1206 | store_savebeta(struct in_cmd* cmd) |
---|
1207 | { |
---|
1208 | struct name_list* nl = cmd->clone->par_names; |
---|
1209 | struct command_parameter_list* pl = cmd->clone->par; |
---|
1210 | int pos; |
---|
1211 | char* name = NULL; |
---|
1212 | // struct command* comm; // not used |
---|
1213 | if (log_val("clear", cmd->clone)) |
---|
1214 | { |
---|
1215 | delete_command_list(savebeta_list); |
---|
1216 | savebeta_list = new_command_list("savebeta_list", 10); |
---|
1217 | delete_command_list(beta0_list); |
---|
1218 | beta0_list = new_command_list("beta0_list", 10); |
---|
1219 | } |
---|
1220 | else { |
---|
1221 | if ((pos = name_list_pos("place", nl)) < 0 || !nl->inform[pos]) { |
---|
1222 | warning("savebeta without place:", "ignored"); |
---|
1223 | return; |
---|
1224 | } |
---|
1225 | if ((pos = name_list_pos("label", nl)) < 0 || !nl->inform[pos] || !(name = pl->parameters[pos]->string)) { |
---|
1226 | warning("savebeta without label:", "ignored"); |
---|
1227 | return; |
---|
1228 | } |
---|
1229 | |
---|
1230 | cmd->clone_flag = 1; /* do not delete */ |
---|
1231 | if ( find_command(name, beta0_list) ) remove_from_command_list(name, beta0_list); |
---|
1232 | add_to_command_list(permbuff(name), cmd->clone, savebeta_list, 0); |
---|
1233 | } |
---|
1234 | } |
---|
1235 | |
---|
1236 | int |
---|
1237 | twiss_input(struct command* tw) |
---|
1238 | /* returns -1 if an invalid beta0 given, |
---|
1239 | returns -1 if only betx or bety given, |
---|
1240 | returns 1 if betx and bety are given, |
---|
1241 | or a valid beta0 (which is then loaded), else 0 */ |
---|
1242 | { |
---|
1243 | struct name_list* nl = tw->par_names; |
---|
1244 | struct command_parameter_list* pl = tw->par; |
---|
1245 | struct command* beta; |
---|
1246 | int i = -1, ret = 0, pos, sb = 0; |
---|
1247 | char* name; |
---|
1248 | double val; |
---|
1249 | pos = name_list_pos("beta0", nl); |
---|
1250 | if (nl->inform[pos] && (name = pl->parameters[pos]->string) != NULL) |
---|
1251 | { |
---|
1252 | if ((pos = name_list_pos(name, beta0_list->list)) > -1) |
---|
1253 | { |
---|
1254 | ret = 1; |
---|
1255 | beta = beta0_list->commands[pos]; |
---|
1256 | do |
---|
1257 | { |
---|
1258 | i++; |
---|
1259 | if (nl->inform[name_list_pos(nl->names[i], nl)] == 0) /* not read */ |
---|
1260 | { |
---|
1261 | if (beta->par->parameters[i]->expr != NULL) |
---|
1262 | val = expression_value(beta->par->parameters[i]->expr, 2); |
---|
1263 | else val = beta->par->parameters[i]->double_value; |
---|
1264 | pl->parameters[i]->double_value = val; |
---|
1265 | nl->inform[name_list_pos(nl->names[i], nl)] = 1; |
---|
1266 | } |
---|
1267 | } |
---|
1268 | while (strcmp(nl->names[i], "energy") != 0); |
---|
1269 | } |
---|
1270 | else ret = -1; |
---|
1271 | } |
---|
1272 | if (ret) return ret; |
---|
1273 | /* if no beta0 given, betx and bety together set inval */ |
---|
1274 | if (nl->inform[name_list_pos("betx", nl)]) sb++; |
---|
1275 | if (nl->inform[name_list_pos("bety", nl)]) sb++; |
---|
1276 | if (sb) |
---|
1277 | { |
---|
1278 | if (sb < 2) return -2; |
---|
1279 | else return 1; |
---|
1280 | } |
---|
1281 | else return 0; |
---|
1282 | } |
---|
1283 | |
---|
1284 | |
---|