1 | #include "madx.h" |
---|
2 | |
---|
3 | static struct constraint* |
---|
4 | new_constraint(int type) |
---|
5 | { |
---|
6 | char rout_name[] = "new_constraint"; |
---|
7 | struct constraint* new = mycalloc(rout_name,1, sizeof(struct constraint)); |
---|
8 | strcpy(new->name, "constraint"); |
---|
9 | new->stamp = 123456; |
---|
10 | if (watch_flag) fprintf(debug_file, "creating ++> %s\n", new->name); |
---|
11 | new->type = type; |
---|
12 | return new; |
---|
13 | } |
---|
14 | |
---|
15 | static void |
---|
16 | grow_constraint_list(struct constraint_list* p) |
---|
17 | { |
---|
18 | char rout_name[] = "grow_constraint_list"; |
---|
19 | struct constraint** c_loc = p->constraints; |
---|
20 | int new = 2*p->max; |
---|
21 | |
---|
22 | p->max = new; |
---|
23 | p->constraints = mycalloc(rout_name, new, sizeof(struct constraint*)); |
---|
24 | for (int j = 0; j < p->curr; j++) p->constraints[j] = c_loc[j]; |
---|
25 | myfree(rout_name, c_loc); |
---|
26 | } |
---|
27 | |
---|
28 | static struct constraint* |
---|
29 | make_constraint(int type, struct command_parameter* par) |
---|
30 | /* makes + stores a constraint from command parameter */ |
---|
31 | { |
---|
32 | struct constraint* new = new_constraint(par->c_type); |
---|
33 | strcpy(new->name, par->name); |
---|
34 | |
---|
35 | switch(par->c_type) |
---|
36 | { |
---|
37 | case 1: /* minimum */ |
---|
38 | case 3: /* both */ |
---|
39 | if (par->min_expr == NULL) new->c_min = par->c_min; |
---|
40 | else |
---|
41 | { |
---|
42 | new->c_min = expression_value(par->min_expr, 2); |
---|
43 | new->ex_c_min = par->min_expr; |
---|
44 | } |
---|
45 | if (par->c_type == 1) break; |
---|
46 | case 2: /* maximum */ |
---|
47 | if (par->max_expr == NULL) new->c_max = par->c_max; |
---|
48 | else |
---|
49 | { |
---|
50 | new->c_max = expression_value(par->max_expr, 2); |
---|
51 | new->ex_c_max = par->max_expr; |
---|
52 | } |
---|
53 | break; |
---|
54 | case 4: /* value */ |
---|
55 | if (par->expr == NULL) new->value = par->double_value; |
---|
56 | else |
---|
57 | { |
---|
58 | new->value = expression_value(par->expr, 2); |
---|
59 | new->ex_value = par->expr; |
---|
60 | } |
---|
61 | } |
---|
62 | if (type == 1) new->weight = command_par_value(new->name, current_weight); |
---|
63 | else new->weight = command_par_value(new->name, current_gweight); |
---|
64 | return new; |
---|
65 | } |
---|
66 | |
---|
67 | static void |
---|
68 | add_to_constraint_list(struct constraint* cs, struct constraint_list* cl) |
---|
69 | /* add constraint cs to the constraint list cl */ |
---|
70 | { |
---|
71 | if (cl->curr == cl->max) grow_constraint_list(cl); |
---|
72 | cl->constraints[cl->curr++] = cs; |
---|
73 | } |
---|
74 | |
---|
75 | // public interface |
---|
76 | |
---|
77 | struct constraint_list* |
---|
78 | new_constraint_list(int length) |
---|
79 | { |
---|
80 | char rout_name[] = "new_constraint_list"; |
---|
81 | struct constraint_list* il |
---|
82 | = (struct constraint_list*) |
---|
83 | mycalloc(rout_name,1, sizeof(struct constraint_list)); |
---|
84 | strcpy(il->name, "constraint_list"); |
---|
85 | il->stamp = 123456; |
---|
86 | if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name); |
---|
87 | il->curr = 0; |
---|
88 | il->max = length; |
---|
89 | il->constraints = (struct constraint**) |
---|
90 | mycalloc(rout_name,length, sizeof(struct constraint*)); |
---|
91 | return il; |
---|
92 | } |
---|
93 | |
---|
94 | struct constraint* |
---|
95 | delete_constraint(struct constraint* cst) |
---|
96 | { |
---|
97 | char rout_name[] = "delete_constraint"; |
---|
98 | if (cst == NULL) return NULL; |
---|
99 | if (stamp_flag && cst->stamp != 123456) |
---|
100 | fprintf(stamp_file, "d_c double delete --> %s\n", cst->name); |
---|
101 | if (watch_flag) fprintf(debug_file, "deleting --> %s\n", "constraint"); |
---|
102 | myfree(rout_name, cst); |
---|
103 | return NULL; |
---|
104 | } |
---|
105 | |
---|
106 | struct constraint_list* |
---|
107 | delete_constraint_list(struct constraint_list* cl) |
---|
108 | { |
---|
109 | char rout_name[] = "delete_constraint_list"; |
---|
110 | if (cl == NULL) return NULL; |
---|
111 | if (stamp_flag && cl->stamp != 123456) |
---|
112 | fprintf(stamp_file, "d_c_l double delete --> %s\n", cl->name); |
---|
113 | if (watch_flag) fprintf(debug_file, "deleting --> %s\n", "constraint_list"); |
---|
114 | myfree(rout_name, cl); |
---|
115 | return NULL; |
---|
116 | } |
---|
117 | |
---|
118 | void |
---|
119 | dump_constraint(struct constraint* c) |
---|
120 | { |
---|
121 | fprintf(prt_file, |
---|
122 | v_format("name: %s type: %I value: %F min: %F max: %F weight: %F\n"), |
---|
123 | c->name, c->type, c->value, c->c_min, c->c_max, c->weight); |
---|
124 | } |
---|
125 | |
---|
126 | #if 0 // not used... |
---|
127 | void |
---|
128 | dump_constraint_list(struct constraint_list* cl) |
---|
129 | { |
---|
130 | int i; |
---|
131 | for (i = 0; i < cl->curr; i++) |
---|
132 | { |
---|
133 | if (cl->constraints[i]) dump_constraint(cl->constraints[i]); |
---|
134 | } |
---|
135 | } |
---|
136 | #endif |
---|
137 | |
---|
138 | #if 0 // not used... |
---|
139 | int |
---|
140 | constraint_name(char* name, int* name_l, int* index) |
---|
141 | /* returns the name of the constraint */ |
---|
142 | { |
---|
143 | int ncp, nbl, len; |
---|
144 | struct constraint* c_c; |
---|
145 | c_c = current_node->cl->constraints[*index]; |
---|
146 | len = strlen(c_c->name); |
---|
147 | ncp = len < *name_l ? len : *name_l; // min(len, *name_l) |
---|
148 | nbl = *name_l - ncp; |
---|
149 | strncpy(name, c_c->name, ncp); |
---|
150 | return 1; |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | void |
---|
155 | fill_constraint_list(int type /* 1 node, 2 global */, |
---|
156 | struct command* cd, struct constraint_list* cl) |
---|
157 | { |
---|
158 | struct command_parameter_list* pl = cd->par; |
---|
159 | struct name_list* nl = cd->par_names; |
---|
160 | struct constraint* l_cons; |
---|
161 | int j; |
---|
162 | for (j = 0; j < pl->curr; j++) |
---|
163 | { |
---|
164 | if (nl->inform[j] && pl->parameters[j]->type == 4) |
---|
165 | { |
---|
166 | l_cons = make_constraint(type, pl->parameters[j]); |
---|
167 | add_to_constraint_list(l_cons, cl); |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | int |
---|
173 | next_constraint(char* name, int* name_l, int* type, double* value, double* c_min, double* c_max, double* weight) |
---|
174 | /* returns the parameters of the next constraint; 0 = none, else count */ |
---|
175 | { |
---|
176 | int i, ncp, nbl; |
---|
177 | struct constraint* c_c; |
---|
178 | int j,k;/* RDM fork */ |
---|
179 | char s, takenextmacro, nomore; /* RDM fork */ |
---|
180 | |
---|
181 | /* RDM fork */ |
---|
182 | if (match_is_on==2) { |
---|
183 | i=match2_cons_curr[0]; |
---|
184 | j=match2_cons_curr[1]; |
---|
185 | k=match2_cons_curr[2]; |
---|
186 | |
---|
187 | if(match2_cons_name[i][j] == NULL) { |
---|
188 | j++; |
---|
189 | |
---|
190 | if(j >= MAX_MATCH_CONS) |
---|
191 | takenextmacro = 1; |
---|
192 | else if (match2_cons_name[i][j]==NULL) // if above is true this one can cause seg fault |
---|
193 | takenextmacro = 1; |
---|
194 | else |
---|
195 | takenextmacro = 0; |
---|
196 | |
---|
197 | if(takenextmacro) { |
---|
198 | i++; j=0; |
---|
199 | |
---|
200 | if(i>=MAX_MATCH_MACRO) |
---|
201 | nomore = 1; |
---|
202 | else if(match2_cons_name[i][j]==NULL) |
---|
203 | nomore = 1; |
---|
204 | else // i,j is the next constraint |
---|
205 | nomore = 0; |
---|
206 | |
---|
207 | if(nomore == 0) { |
---|
208 | name = match2_cons_name[i][j]; |
---|
209 | *name_l = strlen(name); |
---|
210 | *type = 2; /* to be changed according to s or <,=,>*/ |
---|
211 | *value = match2_cons_value[i][j]; |
---|
212 | s = match2_cons_sign[i][j]; |
---|
213 | if (s == '>' && *value > 0) *value=0; |
---|
214 | else if (s == '<' && *value < 0) *value=0; |
---|
215 | c_min = value; /* unknown use */ |
---|
216 | c_max = value; /* unknown use */ |
---|
217 | *weight = 1; /*hardcode no weight with this interface */ |
---|
218 | k++; |
---|
219 | match2_cons_curr[0]=i; |
---|
220 | match2_cons_curr[1]=j; |
---|
221 | match2_cons_curr[2]=k; |
---|
222 | return k; |
---|
223 | } else { |
---|
224 | match2_cons_curr[0]=0; |
---|
225 | match2_cons_curr[1]=0; |
---|
226 | match2_cons_curr[2]=0; |
---|
227 | return 0; |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | else { /* RDM old match */ |
---|
233 | int len; |
---|
234 | |
---|
235 | if (current_node->cl == NULL) return 0; |
---|
236 | |
---|
237 | if (current_node->con_cnt == current_node->cl->curr) { |
---|
238 | current_node->con_cnt = 0; |
---|
239 | return 0; |
---|
240 | } |
---|
241 | |
---|
242 | c_c = current_node->cl->constraints[current_node->con_cnt]; |
---|
243 | len = strlen(c_c->name); |
---|
244 | ncp = len < *name_l ? len : *name_l; |
---|
245 | nbl = *name_l - ncp; |
---|
246 | strncpy(name, c_c->name, ncp); |
---|
247 | |
---|
248 | for (i = 0; i < nbl; i++) |
---|
249 | name[ncp+i] = ' '; |
---|
250 | |
---|
251 | *type = c_c->type; |
---|
252 | |
---|
253 | if (c_c->ex_value == NULL) *value = c_c->value; |
---|
254 | else *value = expression_value(c_c->ex_value,2); |
---|
255 | |
---|
256 | if (c_c->ex_c_min == NULL) *c_min = c_c->c_min; |
---|
257 | else *c_min = expression_value(c_c->ex_c_min,2); |
---|
258 | |
---|
259 | if (c_c->ex_c_max == NULL) *c_max = c_c->c_max; |
---|
260 | else *c_max = expression_value(c_c->ex_c_max,2); |
---|
261 | |
---|
262 | *weight = c_c->weight; |
---|
263 | |
---|
264 | return ++current_node->con_cnt; |
---|
265 | } |
---|
266 | /* RDM fork */ |
---|
267 | return 0; |
---|
268 | } |
---|
269 | |
---|
270 | int |
---|
271 | next_constr_namepos(char* name) |
---|
272 | /* returns the Fortran (!) position of the named variable |
---|
273 | in the opt_fun array of twiss (LD: weak!) */ |
---|
274 | { |
---|
275 | int pos = 0; |
---|
276 | switch (*name) { |
---|
277 | case 'a': |
---|
278 | if (name[3] == 'x') pos = 4; // a??x |
---|
279 | else if (name[3] == 'y') pos = 7; // a??y |
---|
280 | break; |
---|
281 | |
---|
282 | case 'b': |
---|
283 | if (name[3] == 'x') pos = 3; // b??x |
---|
284 | else if (name[3] == 'y') pos = 6; // b??y |
---|
285 | break; |
---|
286 | |
---|
287 | case 'd': |
---|
288 | if (name[1] == 'x') pos = 15; // dx |
---|
289 | else if (name[1] == 'y') pos = 17; // dy |
---|
290 | else if (name[1] == 'p') { |
---|
291 | if (name[2] == 'x') pos = 16; // dpx |
---|
292 | else if (name[2] == 'y') pos = 18; // dpy |
---|
293 | } |
---|
294 | else if (name[1] == 'm') { |
---|
295 | if (name[3] == 'x') pos = 21; // dm?x |
---|
296 | else if (name[3] == 'y') pos = 24; // dm?y |
---|
297 | } |
---|
298 | else if (name[1] == 'd') { |
---|
299 | if (name[2] == 'x') pos = 25; // ddx |
---|
300 | else if (name[2] == 'y') pos = 27; // ddy |
---|
301 | else if (name[2] == 'p') { |
---|
302 | if (name[3] == 'x') pos = 26; // ddpx |
---|
303 | else if (name[3] == 'y') pos = 28; // ddpy |
---|
304 | } |
---|
305 | } |
---|
306 | break; |
---|
307 | |
---|
308 | case 'e': |
---|
309 | pos = 33; // e |
---|
310 | break; |
---|
311 | |
---|
312 | case 'm': |
---|
313 | if (name[2] == 'x') pos = 5; // m?x |
---|
314 | else if (name[2] == 'y') pos = 8; // m?y |
---|
315 | break; |
---|
316 | |
---|
317 | case 'p': |
---|
318 | if (name[1] == 'x') pos = 10; // px |
---|
319 | else if (name[1] == 'y') pos = 12; // py |
---|
320 | else if (name[1] == 't') pos = 14; // pt |
---|
321 | else if (name[1] == 'h') { // was 14 (LD: BUG) |
---|
322 | if (name[3] == 'x') pos = 20; // ph?x |
---|
323 | else if (name[3] == 'y') pos = 23; // ph?y |
---|
324 | } |
---|
325 | break; |
---|
326 | |
---|
327 | case 'r': |
---|
328 | if (name[1] == '1') { |
---|
329 | if (name[2] == '1') pos = 29; // r11 |
---|
330 | else if (name[2] == '2') pos = 30; // r12 |
---|
331 | } |
---|
332 | else if (name[1] == '2') { |
---|
333 | if (name[2] == '1') pos = 31; // r21 |
---|
334 | else if (name[2] == '2') pos = 32; // r22 |
---|
335 | } |
---|
336 | else if (name[1] == 'e') |
---|
337 | /* start mod HG 10.10.2010 - trying to be ascii-independent */ |
---|
338 | pos = (name[2]-'1')*6 + name[3]-'1' + 34; // re11-re66, no range check... |
---|
339 | break; |
---|
340 | |
---|
341 | case 't': |
---|
342 | pos = 13; |
---|
343 | break; |
---|
344 | |
---|
345 | case 'w': |
---|
346 | if (name[1] == 'x') pos = 19; // wx |
---|
347 | else if (name[1] == 'y') pos = 22; // wy |
---|
348 | break; |
---|
349 | |
---|
350 | case 'x': |
---|
351 | pos = 9; // x |
---|
352 | break; |
---|
353 | |
---|
354 | case 'y': // y |
---|
355 | pos = 11; |
---|
356 | break; |
---|
357 | } |
---|
358 | |
---|
359 | return pos; |
---|
360 | } |
---|
361 | |
---|
362 | int |
---|
363 | next_global(char* name, int* name_l, int* type, double* value, double* c_min, double* c_max, double* weight) |
---|
364 | /* returns the parameters of the next global constraint; |
---|
365 | 0 = none, else count */ |
---|
366 | { |
---|
367 | int i, ncp, nbl, len; |
---|
368 | struct constraint* c_c; |
---|
369 | if (current_sequ->cl == NULL) return 0; |
---|
370 | if (current_sequ->con_cnt == current_sequ->cl->curr) |
---|
371 | { |
---|
372 | current_sequ->con_cnt = 0; return 0; |
---|
373 | } |
---|
374 | c_c = current_sequ->cl->constraints[current_sequ->con_cnt]; |
---|
375 | len = strlen(c_c->name); |
---|
376 | ncp = len < *name_l ? len : *name_l; |
---|
377 | nbl = *name_l - ncp; |
---|
378 | strncpy(name, c_c->name, ncp); |
---|
379 | for (i = 0; i < nbl; i++) name[ncp+i] = ' '; |
---|
380 | *type = c_c->type; |
---|
381 | if (c_c->ex_value == NULL) *value = c_c->value; |
---|
382 | else *value = expression_value(c_c->ex_value,2); |
---|
383 | if (c_c->ex_c_min == NULL) *c_min = c_c->c_min; |
---|
384 | else *c_min = expression_value(c_c->ex_c_min,2); |
---|
385 | if (c_c->ex_c_max == NULL) *c_max = c_c->c_max; |
---|
386 | else *c_max = expression_value(c_c->ex_c_max,2); |
---|
387 | *weight = c_c->weight; |
---|
388 | return ++current_sequ->con_cnt; |
---|
389 | } |
---|
390 | |
---|
391 | void |
---|
392 | update_node_constraints(struct node* c_node, struct constraint_list* cl) |
---|
393 | { |
---|
394 | int i, j, k; |
---|
395 | k = 1; set_option("match_local", &k); /* flag */ |
---|
396 | if (c_node->cl == NULL) c_node->cl = new_constraint_list(cl->curr); |
---|
397 | for (j = 0; j < cl->curr; j++) |
---|
398 | { |
---|
399 | k = -1; |
---|
400 | for (i = 0; i < c_node->cl->curr; i++) |
---|
401 | { |
---|
402 | if (strcmp(cl->constraints[j]->name, |
---|
403 | c_node->cl->constraints[i]->name) == 0) k = i; |
---|
404 | } |
---|
405 | if (k < 0) |
---|
406 | { |
---|
407 | if (c_node->cl->curr == c_node->cl->max) |
---|
408 | grow_constraint_list(c_node->cl); |
---|
409 | c_node->cl->constraints[c_node->cl->curr++] = cl->constraints[j]; |
---|
410 | total_const++; |
---|
411 | } |
---|
412 | else c_node->cl->constraints[k] = cl->constraints[j]; |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | void |
---|
417 | update_sequ_constraints(struct sequence* sequ, struct constraint_list* cl) |
---|
418 | { |
---|
419 | int i, j, k; |
---|
420 | if (sequ->cl == NULL) sequ->cl = new_constraint_list(10); |
---|
421 | for (j = 0; j < cl->curr; j++) |
---|
422 | { |
---|
423 | k = -1; |
---|
424 | for (i = 0; i < sequ->cl->curr; i++) |
---|
425 | { |
---|
426 | if (strcmp(cl->constraints[j]->name, |
---|
427 | sequ->cl->constraints[i]->name) == 0) k = i; |
---|
428 | } |
---|
429 | if (k < 0) |
---|
430 | { |
---|
431 | if (sequ->cl->curr == sequ->cl->max) |
---|
432 | grow_constraint_list(sequ->cl); |
---|
433 | sequ->cl->constraints[sequ->cl->curr++] = cl->constraints[j]; |
---|
434 | total_const++; |
---|
435 | } |
---|
436 | else sequ->cl->constraints[k] = cl->constraints[j]; |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | |
---|
441 | |
---|