source: PSPA/madxPSPA/src/mad_match2.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: 23.0 KB
Line 
1#include "madx.h"
2
3// public variables
4
5int MAX_MATCH_CONS =  0; /*these are set to proper values at the initialization of the match2 module*/
6int MAX_MATCH_MACRO = 0; /*zero values means that it is not initialized yet*/
7
8char match2_keepexpressions = 0; /*do not delete expressions at the end matching used by match with PTC knobs*/
9
10char*  **match2_cons_name;
11char   **match2_cons_sign;
12double **match2_cons_value;
13double **match2_cons_value_rhs;
14double **match2_cons_value_lhs;
15double **match2_cons_weight;
16char   **match2_macro_name;
17int      match2_cons_curr[3];
18
19static struct expression* **match2_cons_rhs;
20static struct expression* **match2_cons_lhs;
21
22// private interface
23
24static int
25match2_augmentnconstraints(void)
26{
27  /*makes place in the working arrays for a new macro*/
28  int i,j;
29  char fn[]={"match2_augmentnconstraints"};
30  char* * new_match2_cons_name      = 0x0;
31  double* new_match2_cons_value     = 0x0;
32  double* new_match2_cons_value_rhs = 0x0;
33  double* new_match2_cons_value_lhs = 0x0;
34  double* new_match2_cons_weight    = 0x0;
35  char*   new_match2_cons_sign      = 0x0;
36  struct expression* * new_match2_cons_rhs = 0x0;
37  struct expression* * new_match2_cons_lhs = 0x0;
38
39  if(MAX_MATCH_MACRO == 0)
40  {
41    error("match2_augmentnconstraints","match with use_maco was not initialized");
42    return 1;
43  }
44
45  for(i=0;i<MAX_MATCH_MACRO;i++)
46  {
47    new_match2_cons_name      = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(char*));
48    new_match2_cons_value     = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(double));
49    new_match2_cons_value_rhs = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(double));
50    new_match2_cons_value_lhs = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(double));
51    new_match2_cons_weight    = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(double));
52    new_match2_cons_sign      = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(char));
53    new_match2_cons_rhs       = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(struct expression*));
54    new_match2_cons_lhs       = mycalloc(fn,MAX_MATCH_CONS*2,sizeof(struct expression*));
55
56    /*copy old content to the new arrays*/
57    for(j=0;j<MAX_MATCH_CONS;j++)
58    {
59      new_match2_cons_name     [j] = match2_cons_name     [i][j];
60      new_match2_cons_value    [j] = match2_cons_value    [i][j];
61      new_match2_cons_value_lhs[j] = match2_cons_value_lhs[i][j];
62      new_match2_cons_value_rhs[j] = match2_cons_value_rhs[i][j];
63      new_match2_cons_weight   [j] = match2_cons_weight   [i][j];
64      new_match2_cons_sign     [j] = match2_cons_sign     [i][j];
65      new_match2_cons_rhs      [j] = match2_cons_rhs      [i][j];
66      new_match2_cons_lhs      [j] = match2_cons_lhs      [i][j];
67    }
68
69    /*initializes the new parts*/
70    for(j=MAX_MATCH_CONS;j<MAX_MATCH_CONS*2;j++)
71    {
72      new_match2_cons_name     [j] = 0x0;
73      new_match2_cons_value    [j] = 0.0;
74      new_match2_cons_value_lhs[j] = 0.0;
75      new_match2_cons_value_rhs[j] = 0.0;
76      new_match2_cons_weight   [j] = 0.0;
77      new_match2_cons_sign     [j] = 'n';
78      new_match2_cons_rhs      [j] = 0x0;
79      new_match2_cons_lhs      [j] = 0x0;
80    }
81
82    /*free the old arrays*/
83    myfree(fn,match2_cons_name     [i]);
84    myfree(fn,match2_cons_value    [i]);
85    myfree(fn,match2_cons_value_lhs[i]);
86    myfree(fn,match2_cons_value_rhs[i]);
87    myfree(fn,match2_cons_weight   [i]);
88    myfree(fn,match2_cons_sign     [i]);
89    myfree(fn,match2_cons_rhs      [i]);
90    myfree(fn,match2_cons_lhs      [i]);
91
92    /*assign freed pointers to the new arrays*/
93    match2_cons_name     [i] = new_match2_cons_name ;
94    match2_cons_value    [i] = new_match2_cons_value ;
95    match2_cons_value_lhs[i] = new_match2_cons_value_lhs ;
96    match2_cons_value_rhs[i] = new_match2_cons_value_rhs ;
97    match2_cons_weight   [i] = new_match2_cons_weight ;
98    match2_cons_sign     [i] = new_match2_cons_sign ;
99    match2_cons_rhs      [i] = new_match2_cons_rhs ;
100    match2_cons_lhs      [i] = new_match2_cons_lhs ;
101  }
102
103  MAX_MATCH_CONS =  MAX_MATCH_CONS*2;
104
105  return MAX_MATCH_CONS;
106}
107
108static void
109match2_alloc_arrays(void)
110{
111  int i;
112  char fn[]={"match2_alloc_arrays"};
113
114  match2_macro_name     = mycalloc(fn,MAX_MATCH_MACRO,sizeof(char*));
115  match2_cons_name      = mycalloc(fn,MAX_MATCH_MACRO,sizeof(char**));
116  match2_cons_value     = mycalloc(fn,MAX_MATCH_MACRO,sizeof(double*));
117  match2_cons_value_rhs = mycalloc(fn,MAX_MATCH_MACRO,sizeof(double*));
118  match2_cons_value_lhs = mycalloc(fn,MAX_MATCH_MACRO,sizeof(double*));
119  match2_cons_weight    = mycalloc(fn,MAX_MATCH_MACRO,sizeof(double*));
120  match2_cons_sign      = mycalloc(fn,MAX_MATCH_MACRO,sizeof(char*));
121  match2_cons_rhs       = mycalloc(fn,MAX_MATCH_MACRO,sizeof(struct expression**));
122  match2_cons_lhs       = mycalloc(fn,MAX_MATCH_MACRO,sizeof(struct expression**));
123
124  for(i=0;i<MAX_MATCH_MACRO;i++)
125  {
126    match2_cons_name[i]      = mycalloc(fn,MAX_MATCH_CONS,sizeof(char*));
127    match2_cons_value[i]     = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
128    match2_cons_value_rhs[i] = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
129    match2_cons_value_lhs[i] = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
130    match2_cons_weight[i]    = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
131    match2_cons_sign[i]      = mycalloc(fn,MAX_MATCH_CONS,sizeof(char));
132    match2_cons_rhs[i]       = mycalloc(fn,MAX_MATCH_CONS,sizeof(struct expression*));
133    match2_cons_lhs[i]       = mycalloc(fn,MAX_MATCH_CONS,sizeof(struct expression*));
134  }
135
136}
137
138static void
139match2_init_arrays(void)
140{
141  /*clean the stuff;*/
142  int i,j;
143
144  for(i=0;i<MAX_MATCH_MACRO;i++)
145  {
146    match2_macro_name[i]=NULL;
147
148    for(j=0;j<MAX_MATCH_CONS;j++)
149    {
150      match2_cons_name     [i][j]=0x0;
151      match2_cons_value    [i][j]=0.0;
152      match2_cons_value_lhs[i][j]=0.0;
153      match2_cons_value_rhs[i][j]=0.0;
154      match2_cons_weight   [i][j]=0.0;
155      match2_cons_sign     [i][j]='n';
156      match2_cons_rhs      [i][j]=0x0;
157      match2_cons_lhs      [i][j]=0x0;
158    }
159  }
160
161  for(i=0;i<3;i++) match2_cons_curr[i]=0;
162}
163
164static void
165match2_setconstrinrange(struct node** nodes, double w, char* parname, char s, char* rexpr)
166{
167/* sets the same consraint for a range of elements defined bu nodes */
168  struct node* c_node;
169  char tablecmd[500];
170  char buff[500];
171  char* p;
172
173  c_node = nodes[0];
174  do
175  {
176    strcpy(buff,c_node->name);
177
178    p = strstr(buff,":");
179    if ( p )
180    {
181      if (p[1] == '0') /*it means that this is a drift automatically added to a sequence*/
182      {               /*this guy does not work with table command so we do not care about him*/
183        c_node = c_node->next;
184        continue;
185      }
186      p[0] = '[';
187      p[2] = ']';
188      p[3] =  0;
189    }
190
191    sprintf(tablecmd,"constraint, weight=%f, expr=table(twiss,%s,%s)%c%s ;",
192            w,  buff, parname, s, rexpr);
193
194    pro_input(tablecmd);
195
196    if (nodes[1] == c_node) break; /*only one element in the range*/
197
198    c_node = c_node->next;
199  } while ( c_node && (c_node != nodes[1]) );
200}
201
202static void
203match2_disasambleconstraint(struct in_cmd* cmd)
204{
205  /*Disassambles regular constraint with range into set of constraints with expr=...*/
206  struct node* nodes[2];
207  struct sequence* sequ;
208  char* name;
209  int k,  jj;
210  struct command_parameter_list* pl = cmd->clone->par;
211  struct name_list* nl = cmd->clone->par_names;
212  struct command_parameter* par = 0x0;
213  char s;
214  char buff[50];
215  double w;
216  sequ = current_sequ;
217  name = command_par_string("range",cmd->clone);
218
219  if ( strlen(name) > 0 )  /* parameter has been read */
220  {
221    k = get_ex_range(name, sequ, nodes);
222    if (k == 0)
223    {
224      error("match2_disasambleconstraint","Bad range! Ignoring\n");
225      return;
226    }
227  }
228  else
229  {
230    printf("Range not specified explicitely, using FULL range.\n");
231    nodes[0] = sequ->ex_start;
232    nodes[1] = sequ->ex_end;
233  }
234
235  w = command_par_value("weight",cmd->clone);
236  for (jj = 0; jj < pl->curr; jj++)
237  {
238    if (nl->inform[jj] && pl->parameters[jj]->type == 4)
239    {
240      par = pl->parameters[jj];
241/*
242  printf("Got constraint type %d name %s\n",par->c_type, par->name);
243  printf("   min_expr: %#x  c_min=%f \n", par->min_expr, par->c_min);
244  printf("   max_expr: %#x  c_max=%f \n", par->max_expr, par->c_max);
245  printf("       expr: %#x  double_value %f \n\n", par->expr, par->double_value);
246*/
247
248      switch(par->c_type)
249      {
250        case 1: /* minimum */
251        case 3: /* both */
252          s = '>';
253          if (par->min_expr == NULL)
254          {
255            sprintf(buff,"%f",par->c_min);
256            match2_setconstrinrange(nodes,w, par->name,s,buff);
257          }
258          else
259          {
260            match2_setconstrinrange(nodes,w, par->name,s,par->min_expr->string);
261          }
262          if (par->c_type == 1) break;
263        case 2: /* maximum */
264          s = '<';
265          if (par->max_expr == NULL)
266          {
267            sprintf(buff,"%f",par->c_max);
268            match2_setconstrinrange(nodes,w, par->name,s,buff);
269          }
270          else
271          {
272            match2_setconstrinrange(nodes,w, par->name,s,par->max_expr->string);
273          }
274          break;
275        case 4: /* value */
276          s = '=';
277          if (par->expr == NULL)
278          {
279            sprintf(buff,"%f",par->double_value);
280            match2_setconstrinrange(nodes,w, par->name,s,buff);
281          }
282          else
283          {
284            match2_setconstrinrange(nodes,w, par->name,s,par->expr->string);
285          }
286      }
287    }
288  }
289}
290
291// public interface
292
293int
294match2_augmentnmacros(void)
295{
296  /* makes place in the working arrays for a new macro */
297  int i,j;
298  char fn[]={"match2_augmentnmacros"};
299  char   **new_match2_macro_name;
300  char*  **new_match2_cons_name;
301  double **new_match2_cons_value;
302  double **new_match2_cons_value_rhs;
303  double **new_match2_cons_value_lhs;
304  double **new_match2_cons_weight;
305  char   **new_match2_cons_sign;
306  struct expression* **new_match2_cons_rhs;
307  struct expression* **new_match2_cons_lhs;
308
309  if(MAX_MATCH_MACRO == 0)
310  {
311    error("match2_augmentnconstraints","match with use_maco was not initialized");
312    return 1;
313  }
314
315  new_match2_macro_name     = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(char*));
316  new_match2_cons_name      = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(char**));
317  new_match2_cons_value     = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*));
318  new_match2_cons_value_rhs = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*));
319  new_match2_cons_value_lhs = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*));
320  new_match2_cons_weight    = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*));
321  new_match2_cons_sign      = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(char*));
322  new_match2_cons_rhs       = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(struct expression**));
323  new_match2_cons_lhs       = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(struct expression**));
324
325  /*copy old pointers to arrays*/
326  for(i=0;i<MAX_MATCH_MACRO;i++)
327  {
328    new_match2_macro_name[i]     = match2_macro_name[i];
329    new_match2_cons_name[i]      = match2_cons_name[i];
330    new_match2_cons_value[i]     = match2_cons_value[i];
331    new_match2_cons_value_rhs[i] = match2_cons_value_rhs[i];
332    new_match2_cons_value_lhs[i] = match2_cons_value_lhs[i];
333    new_match2_cons_weight[i]    = match2_cons_weight[i];
334    new_match2_cons_sign[i]      = match2_cons_sign[i];
335    new_match2_cons_rhs[i]       = match2_cons_rhs[i];
336    new_match2_cons_lhs[i]       = match2_cons_lhs[i];
337  }
338
339  /*free the old arrays*/
340  myfree(fn,match2_macro_name);
341  myfree(fn,match2_cons_name);
342  myfree(fn,match2_cons_value);
343  myfree(fn,match2_cons_value_rhs);
344  myfree(fn,match2_cons_value_lhs);
345  myfree(fn,match2_cons_weight);
346  myfree(fn,match2_cons_sign);
347  myfree(fn,match2_cons_rhs);
348  myfree(fn,match2_cons_lhs);
349
350  /*assign freed pointers to the new arrays*/
351  match2_macro_name     = new_match2_macro_name;
352  match2_cons_name      = new_match2_cons_name;
353  match2_cons_value     = new_match2_cons_value;
354  match2_cons_value_rhs = new_match2_cons_value_rhs;
355  match2_cons_value_lhs = new_match2_cons_value_lhs;
356  match2_cons_weight    = new_match2_cons_weight;
357  match2_cons_sign      = new_match2_cons_sign;
358  match2_cons_rhs       = new_match2_cons_rhs;
359  match2_cons_lhs       = new_match2_cons_lhs;
360
361  /*make arrays in the new row*/
362  match2_cons_name[MAX_MATCH_MACRO]      = mycalloc(fn,MAX_MATCH_CONS,sizeof(char*));
363  match2_cons_value[MAX_MATCH_MACRO]     = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
364  match2_cons_value_rhs[MAX_MATCH_MACRO] = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
365  match2_cons_value_lhs[MAX_MATCH_MACRO] = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
366  match2_cons_weight[MAX_MATCH_MACRO]    = mycalloc(fn,MAX_MATCH_CONS,sizeof(double));
367  match2_cons_sign[MAX_MATCH_MACRO]      = mycalloc(fn,MAX_MATCH_CONS,sizeof(char));
368  match2_cons_rhs[MAX_MATCH_MACRO]       = mycalloc(fn,MAX_MATCH_CONS,sizeof(struct expression*));
369  match2_cons_lhs[MAX_MATCH_MACRO]       = mycalloc(fn,MAX_MATCH_CONS,sizeof(struct expression*));
370
371  /*initializes arrays in the last row*/
372  match2_macro_name[MAX_MATCH_MACRO]=NULL;
373
374  for(j=0;j<MAX_MATCH_CONS;j++)
375  {
376    match2_cons_name     [MAX_MATCH_MACRO][j]=0x0;
377    match2_cons_value    [MAX_MATCH_MACRO][j]=0.0;
378    match2_cons_value_lhs[MAX_MATCH_MACRO][j]=0.0;
379    match2_cons_value_rhs[MAX_MATCH_MACRO][j]=0.0;
380    match2_cons_weight   [MAX_MATCH_MACRO][j]=0.0;
381    match2_cons_sign     [MAX_MATCH_MACRO][j]='n';
382    match2_cons_rhs      [MAX_MATCH_MACRO][j]=0x0;
383    match2_cons_lhs      [MAX_MATCH_MACRO][j]=0x0;
384  }
385
386  return ++MAX_MATCH_MACRO;
387}
388
389void
390match2_delete_arrays(void)
391{
392  /*clean the stuff;*/
393  int i;
394  char fn[]={"match2_delete_arrays"};
395
396  if(MAX_MATCH_MACRO <= 0) return;
397
398  for(i=0;i<MAX_MATCH_MACRO;i++)
399  {
400    if(match2_cons_name[i] == 0x0) break;
401    myfree(fn,match2_cons_name     [i]);
402    myfree(fn,match2_cons_value    [i]);
403    myfree(fn,match2_cons_value_lhs[i]);
404    myfree(fn,match2_cons_value_rhs[i]);
405    myfree(fn,match2_cons_weight   [i]);
406    myfree(fn,match2_cons_sign     [i]);
407    myfree(fn,match2_cons_rhs      [i]);
408    myfree(fn,match2_cons_lhs      [i]);
409  }
410
411  myfree(fn,match2_macro_name);
412  myfree(fn,match2_cons_name);
413  myfree(fn,match2_cons_value);
414  myfree(fn,match2_cons_value_rhs);
415  myfree(fn,match2_cons_value_lhs);
416  myfree(fn,match2_cons_weight);
417  myfree(fn,match2_cons_sign);
418  myfree(fn,match2_cons_rhs);
419  myfree(fn,match2_cons_lhs);
420
421  match2_macro_name     = 0x0;
422  match2_cons_name      = 0x0;
423  match2_cons_value     = 0x0;
424  match2_cons_value_rhs = 0x0;
425  match2_cons_value_lhs = 0x0;
426  match2_cons_weight    = 0x0;
427  match2_cons_sign      = 0x0;
428  match2_cons_rhs       = 0x0;
429  match2_cons_lhs       = 0x0;
430
431  /*for security so we cannot add more constraints if the module is not initialized*/
432  MAX_MATCH_CONS =  0;
433  MAX_MATCH_MACRO = 0;
434}
435
436void
437match2_delete_expressions(void)
438{
439  char rout_name[] = "match2_delete_expressions";
440
441  int i,j;
442
443  for(i=0;i<MAX_MATCH_MACRO;i++)
444  {
445    if ( match2_cons_name[i][0] == 0x0) break;
446    for(j=0;j<MAX_MATCH_CONS;j++)
447    {
448      if ( match2_cons_name[i][j] == 0x0) break;
449      myfree(rout_name,match2_cons_name[i][j]);
450      delete_expression(match2_cons_rhs[i][j]);
451      delete_expression(match2_cons_lhs[i][j]);
452      match2_cons_rhs[i][j] = 0x0;
453      match2_cons_lhs[i][j] = 0x0;
454    }
455  }
456}
457
458void
459match2_match(struct in_cmd* cmd)
460{
461  (void)cmd;
462
463  match_is_on = 2;
464  total_const=0;
465
466  if (MAX_MATCH_MACRO == 0)
467  {
468    MAX_MATCH_CONS =  100;
469    MAX_MATCH_MACRO = 1;
470    match2_alloc_arrays();
471  }
472  else
473  {
474    match2_delete_expressions();
475  }
476
477  match2_init_arrays();
478}
479
480void
481match2_end(struct in_cmd* cmd)
482{
483  int i,j;
484
485  fprintf(prt_file, "\n");
486  fprintf(prt_file, "MATCH SUMMARY\n\n");
487/*  fprintf(prt_file, "Macro Constraint            Value                     Penalty\n");*/
488  fprintf(prt_file, "--------------------------------------------------------------------\n");
489  penalty=0;
490  for(i=0;i<MAX_MATCH_MACRO;i++)
491  {
492    if(match2_macro_name[i]==NULL) break;
493    fprintf(prt_file,"macro: %-20s\n",match2_macro_name[i]);
494    for(j=0;j<MAX_MATCH_CONS;j++)
495    {
496      if (match2_cons_name[i][j]==NULL) break;
497      fprintf(prt_file,"  constraint: %-40s\n",match2_cons_name[i][j]);
498      fprintf(prt_file,"  values:     %+12.5e%c%+12.5e\n",
499              match2_cons_value_lhs[i][j],
500              match2_cons_sign[i][j],
501              match2_cons_value_rhs[i][j]);
502      fprintf(prt_file,"  weight:     %+12.5e\n", match2_cons_weight[i][j]);
503      fprintf(prt_file,"  penalty:    %+12.5e\n\n",match2_cons_value[i][j]);
504      penalty+=pow(match2_cons_value[i][j],2);
505    }
506  }
507
508  fprintf(prt_file, "\n\n");
509  fprintf(prt_file, "Final Penalty Function = %16.8e\n\n",penalty);
510  match2_print_var(cmd);
511  fprintf(prt_file, "END MATCH SUMMARY\n\n");
512  current_gweight = delete_command(current_gweight);
513  current_weight = delete_command(current_weight);
514  vary_vect = delete_double_array(vary_vect);
515  vary_dvect = delete_double_array(vary_dvect);
516  fun_vect = delete_double_array(fun_vect);
517  comm_constraints->curr = 0;
518  stored_match_var = delete_command_list(stored_match_var);
519  vary_cnt = 0;
520  match_is_on = 0;
521  current_call_lim = 0;
522  current_calls = 0;
523  set_option("twiss_print", &keep_tw_print);
524  print_match_summary = 0;
525  set_option("match_summary", &print_match_summary);
526
527
528  fprintf(prt_file, "VARIABLE \"TAR\" SET TO %16.8e\n",penalty);
529/*  sprintf(assign_cmd,"tar= %16.8e ;",penalty);*/
530/*  pro_input(assign_cmd);*/
531  set_variable("tar",&penalty);
532
533  if (!match2_keepexpressions)
534  {
535    match2_delete_expressions();
536    match2_delete_arrays();
537    total_const = 0;
538  }
539}
540
541void
542match2_macro(struct in_cmd* cmd)
543{
544  int pos;
545  struct name_list* nl = cmd->clone->par_names;
546  struct command_parameter_list* pl = cmd->clone->par;
547  int i, idx = -1;
548
549
550  pos = name_list_pos("name", nl);
551  if (nl->inform[pos])
552  {
553
554    for(i=0; i < MAX_MATCH_MACRO;i++)
555    {
556      if (match2_macro_name[i]==NULL)
557      {
558        idx = i;
559        break;
560      }
561    }
562
563    if (idx < 0  )
564    {
565      printf("Max number of match macros reached. Augmenting.\n");
566      match2_augmentnmacros();
567      idx = MAX_MATCH_MACRO -1;
568    }
569/*    printf("%d\n",i);*/
570    match2_macro_name[idx]=pl->parameters[pos]->string;
571/*
572  printf("%d: exec, %s;\n",idx,pl->parameters[pos]->string);
573  printf("%s\n", execute);*/
574    /*      pro_input(execute);*/
575  }
576}
577
578void
579match2_constraint(struct in_cmd* cmd)
580{
581  int i,j,k,nitem; // ,type; // not used
582  int start,end;
583  char **toks=cmd->tok_list->p;
584  int n = cmd->tok_list->curr;
585  struct expression* expr = NULL;
586  char* cname;
587  char s;
588
589
590  int exprfound = 0;
591
592  i=0;j=0;s='n';
593
594
595  for(i=0;i<MAX_MATCH_MACRO;i++)
596  {
597    if (match2_macro_name[i]==NULL)
598    {
599      break;
600    }
601  }
602
603  i--;
604
605  for(j=0;j<MAX_MATCH_CONS;j++)
606  {
607    if (match2_cons_lhs[i][j]==NULL)
608    {
609      break;
610    }
611  }
612
613
614  if (j >= MAX_MATCH_CONS)
615  {
616    j=MAX_MATCH_CONS;
617    printf("Max number of constraints %d reached. Increasing tables. Macro %d \n",MAX_MATCH_CONS, i);
618    match2_augmentnconstraints();
619  }
620
621  exprfound = 0;
622  for(start=0; start<n; start++)
623  {
624    if (strcmp(toks[start],"expr")==0)
625    {
626      exprfound = 1;
627      break;
628    }
629  }
630
631  /* the ckeck if "expr" is present should be here? /skowron/ */
632  if (exprfound == 0)
633  {
634    match2_disasambleconstraint(cmd);
635    return;
636  }
637
638  start=start+2;
639/*  start=3; |+constraint [0], expr [1] = [2] start [3]+|*/
640  /*  printf("%s\n",toks[start]);*/
641  for (k = start; k < n; k++) {
642    s=*toks[k];
643    if (s == '<' || s == '>' || s == '=')
644    {
645      break;
646    }
647  }
648
649  if (k>=n)
650  {
651    warning("match2_constraint: expr not present in this constraint","ignoring");
652    return;
653  }
654
655  /*  printf("%d\n",k);*/
656  if (loc_expr(toks, k, start, &end) > 0) // (type =  // not used
657  {
658    nitem = end + 1 - start;
659
660    expr=make_expression(nitem,&toks[start]);
661
662
663    /*      printf("%d %d\n",i,j);*/
664    match2_cons_lhs[i][j]=expr;
665    match2_cons_sign[i][j]=s;
666    /*      comm->par->parameters[pos]->type=4;*/
667    /*      comm->par->parameters[pos]->expr=expr;*/
668  }
669  else
670  {
671    warning("match2_constraint: no valid expression in constraint","ignoring");
672  }
673
674
675  start=end+2;
676  for (k = start; k < n; k++)
677  {
678    if (*toks[k] == ';')
679    {
680      break;
681    }
682  }
683
684  if (loc_expr(toks, k, start, &end) > 0) // (type = // not used
685  {
686    nitem = end + 1 - start;
687    expr=make_expression(nitem,&toks[start]);
688    match2_cons_rhs[i][j]=expr;
689  }
690  else
691  {
692    match2_cons_lhs[i][j]=NULL;
693    match2_cons_rhs[i][j]=NULL;
694    warning("no valid expression in constraint","ignoring");
695    return;
696  }
697
698
699  match2_cons_weight[i][j]=command_par_value("weight",cmd->clone);
700  for(start=0; start<n; start++)
701  {
702    if (strcmp(toks[start],"expr")==0)
703    {
704      break;
705    }
706  }
707
708  start=start+2;
709  nitem = end-start+1;
710  if ( (cname=command_par_string("name",cmd->clone) ) == NULL) {
711/*    printf("not-given-name\n");*/
712    cname=spec_join(&toks[start], nitem);
713  } else {
714/*    printf("given-name %s\n",cname);*/
715  };
716  match2_cons_name[i][j]=(char*) mymalloc("match2_constraint",strlen(cname)+1);
717/*  strcpy(match2_cons_name[i][j],cname);*/
718  n=0;
719  {
720    int len = strlen(cname);
721    for(k=0;k<len;k++){
722      if(cname[k]!=' ') {
723        match2_cons_name[i][j][n]=cname[k];
724        n++;
725      }
726    }
727  }
728  match2_cons_name[i][j][n]='\0';
729/*  strcpy(match2_cons_name[i][j],cname);*/
730  total_const++;
731/*
732  printf("%d %d, ntot=%d: %s\n",i,j,total_const,match2_cons_name[i][j]);
733  printf("%e %c %e\n",expression_value(match2_cons_lhs[i][j],type) , match2_cons_sign[i][j],
734  expression_value(match2_cons_rhs[i][j],type) );
735*/
736}
737
738int
739match2_evaluate_exressions(int i, int k, double* fun_vec)
740{
741  int j;
742  double rhs,lhs,r;
743  char s;
744  for(j=0; j < MAX_MATCH_CONS ;j++) {
745
746    if (match2_cons_name[i][j]==NULL) break;
747
748    rhs=expression_value(match2_cons_rhs[i][j],2);
749    lhs=expression_value(match2_cons_lhs[i][j],2);
750    s =match2_cons_sign[i][j];
751    r=lhs - rhs;
752    fun_vec[k]=match2_cons_weight[i][j]*r;
753    if (s == '>' && r > 0) fun_vec[k]=0;
754    else if (s == '<'  && r < 0) fun_vec[k]=0;
755    match2_cons_value[i][j]=fun_vec[k];
756    match2_cons_value_rhs[i][j]=rhs;
757    match2_cons_value_lhs[i][j]=lhs;
758    k++;
759  }
760
761  return k;
762}
763
764int
765match2_print_var(struct in_cmd* cmd)
766{
767  int n,l;
768  char *varname,*knobfilename,*knobname;
769  double ivalue,fvalue;
770  FILE *knobfile=NULL;
771  knobfilename=command_par_string("knobfile",cmd->clone);
772  if (knobfilename){
773      knobfile=fopen(knobfilename,"w");
774  };
775  n=stored_match_var->curr;
776  fprintf(prt_file, "\n\n");
777  fprintf(prt_file, "%-24s %-12s %-12s %-12s %-12s\n",
778                    "Variable", "Final Value",
779                    "Initial Value","Lower Limit",
780                    "Upper Limit");
781  for(l=0;l<80;l++) fprintf(prt_file, "-");
782  fprintf(prt_file,"\n");
783  for(l=0;l<n;l++){
784    varname=command_par_string("name",stored_match_var->commands[l]);
785    ivalue=command_par_value("init",stored_match_var->commands[l]);
786    fvalue=get_variable(varname);
787    knobname=command_par_string("knob",stored_match_var->commands[l]);
788    if (knobfilename){
789      fprintf(knobfile, "%-12s :=%+15.8e%+15.8e*%s;\n",
790                         varname,ivalue,fvalue-ivalue,knobname);
791    }
792    fprintf(prt_file,"%-24s",varname);
793    fprintf(prt_file," %12.5e",fvalue);
794    fprintf(prt_file," %12.5e",ivalue);
795    fprintf(prt_file," %12.5e",command_par_value("lower",stored_match_var->commands[l]));
796    fprintf(prt_file," %12.5e",command_par_value("upper",stored_match_var->commands[l]));
797    fprintf(prt_file,"\n");
798  }
799  fprintf(prt_file,"\n");
800  if ( knobfilename ) fclose(knobfile);
801  return 0;
802}
Note: See TracBrowser for help on using the repository browser.