#include "madx.h" // public variables int MAX_MATCH_CONS = 0; /*these are set to proper values at the initialization of the match2 module*/ int MAX_MATCH_MACRO = 0; /*zero values means that it is not initialized yet*/ char match2_keepexpressions = 0; /*do not delete expressions at the end matching used by match with PTC knobs*/ char* **match2_cons_name; char **match2_cons_sign; double **match2_cons_value; double **match2_cons_value_rhs; double **match2_cons_value_lhs; double **match2_cons_weight; char **match2_macro_name; int match2_cons_curr[3]; static struct expression* **match2_cons_rhs; static struct expression* **match2_cons_lhs; // private interface static int match2_augmentnconstraints(void) { /*makes place in the working arrays for a new macro*/ int i,j; char fn[]={"match2_augmentnconstraints"}; char* * new_match2_cons_name = 0x0; double* new_match2_cons_value = 0x0; double* new_match2_cons_value_rhs = 0x0; double* new_match2_cons_value_lhs = 0x0; double* new_match2_cons_weight = 0x0; char* new_match2_cons_sign = 0x0; struct expression* * new_match2_cons_rhs = 0x0; struct expression* * new_match2_cons_lhs = 0x0; if(MAX_MATCH_MACRO == 0) { error("match2_augmentnconstraints","match with use_maco was not initialized"); return 1; } for(i=0;iname); p = strstr(buff,":"); if ( p ) { if (p[1] == '0') /*it means that this is a drift automatically added to a sequence*/ { /*this guy does not work with table command so we do not care about him*/ c_node = c_node->next; continue; } p[0] = '['; p[2] = ']'; p[3] = 0; } sprintf(tablecmd,"constraint, weight=%f, expr=table(twiss,%s,%s)%c%s ;", w, buff, parname, s, rexpr); pro_input(tablecmd); if (nodes[1] == c_node) break; /*only one element in the range*/ c_node = c_node->next; } while ( c_node && (c_node != nodes[1]) ); } static void match2_disasambleconstraint(struct in_cmd* cmd) { /*Disassambles regular constraint with range into set of constraints with expr=...*/ struct node* nodes[2]; struct sequence* sequ; char* name; int k, jj; struct command_parameter_list* pl = cmd->clone->par; struct name_list* nl = cmd->clone->par_names; struct command_parameter* par = 0x0; char s; char buff[50]; double w; sequ = current_sequ; name = command_par_string("range",cmd->clone); if ( strlen(name) > 0 ) /* parameter has been read */ { k = get_ex_range(name, sequ, nodes); if (k == 0) { error("match2_disasambleconstraint","Bad range! Ignoring\n"); return; } } else { printf("Range not specified explicitely, using FULL range.\n"); nodes[0] = sequ->ex_start; nodes[1] = sequ->ex_end; } w = command_par_value("weight",cmd->clone); for (jj = 0; jj < pl->curr; jj++) { if (nl->inform[jj] && pl->parameters[jj]->type == 4) { par = pl->parameters[jj]; /* printf("Got constraint type %d name %s\n",par->c_type, par->name); printf(" min_expr: %#x c_min=%f \n", par->min_expr, par->c_min); printf(" max_expr: %#x c_max=%f \n", par->max_expr, par->c_max); printf(" expr: %#x double_value %f \n\n", par->expr, par->double_value); */ switch(par->c_type) { case 1: /* minimum */ case 3: /* both */ s = '>'; if (par->min_expr == NULL) { sprintf(buff,"%f",par->c_min); match2_setconstrinrange(nodes,w, par->name,s,buff); } else { match2_setconstrinrange(nodes,w, par->name,s,par->min_expr->string); } if (par->c_type == 1) break; case 2: /* maximum */ s = '<'; if (par->max_expr == NULL) { sprintf(buff,"%f",par->c_max); match2_setconstrinrange(nodes,w, par->name,s,buff); } else { match2_setconstrinrange(nodes,w, par->name,s,par->max_expr->string); } break; case 4: /* value */ s = '='; if (par->expr == NULL) { sprintf(buff,"%f",par->double_value); match2_setconstrinrange(nodes,w, par->name,s,buff); } else { match2_setconstrinrange(nodes,w, par->name,s,par->expr->string); } } } } } // public interface int match2_augmentnmacros(void) { /* makes place in the working arrays for a new macro */ int i,j; char fn[]={"match2_augmentnmacros"}; char **new_match2_macro_name; char* **new_match2_cons_name; double **new_match2_cons_value; double **new_match2_cons_value_rhs; double **new_match2_cons_value_lhs; double **new_match2_cons_weight; char **new_match2_cons_sign; struct expression* **new_match2_cons_rhs; struct expression* **new_match2_cons_lhs; if(MAX_MATCH_MACRO == 0) { error("match2_augmentnconstraints","match with use_maco was not initialized"); return 1; } new_match2_macro_name = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(char*)); new_match2_cons_name = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(char**)); new_match2_cons_value = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*)); new_match2_cons_value_rhs = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*)); new_match2_cons_value_lhs = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*)); new_match2_cons_weight = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(double*)); new_match2_cons_sign = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(char*)); new_match2_cons_rhs = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(struct expression**)); new_match2_cons_lhs = mycalloc(fn,MAX_MATCH_MACRO+1,sizeof(struct expression**)); /*copy old pointers to arrays*/ for(i=0;icurr = 0; stored_match_var = delete_command_list(stored_match_var); vary_cnt = 0; match_is_on = 0; current_call_lim = 0; current_calls = 0; set_option("twiss_print", &keep_tw_print); print_match_summary = 0; set_option("match_summary", &print_match_summary); fprintf(prt_file, "VARIABLE \"TAR\" SET TO %16.8e\n",penalty); /* sprintf(assign_cmd,"tar= %16.8e ;",penalty);*/ /* pro_input(assign_cmd);*/ set_variable("tar",&penalty); if (!match2_keepexpressions) { match2_delete_expressions(); match2_delete_arrays(); total_const = 0; } } void match2_macro(struct in_cmd* cmd) { int pos; struct name_list* nl = cmd->clone->par_names; struct command_parameter_list* pl = cmd->clone->par; int i, idx = -1; pos = name_list_pos("name", nl); if (nl->inform[pos]) { for(i=0; i < MAX_MATCH_MACRO;i++) { if (match2_macro_name[i]==NULL) { idx = i; break; } } if (idx < 0 ) { printf("Max number of match macros reached. Augmenting.\n"); match2_augmentnmacros(); idx = MAX_MATCH_MACRO -1; } /* printf("%d\n",i);*/ match2_macro_name[idx]=pl->parameters[pos]->string; /* printf("%d: exec, %s;\n",idx,pl->parameters[pos]->string); printf("%s\n", execute);*/ /* pro_input(execute);*/ } } void match2_constraint(struct in_cmd* cmd) { int i,j,k,nitem; // ,type; // not used int start,end; char **toks=cmd->tok_list->p; int n = cmd->tok_list->curr; struct expression* expr = NULL; char* cname; char s; int exprfound = 0; i=0;j=0;s='n'; for(i=0;i= MAX_MATCH_CONS) { j=MAX_MATCH_CONS; printf("Max number of constraints %d reached. Increasing tables. Macro %d \n",MAX_MATCH_CONS, i); match2_augmentnconstraints(); } exprfound = 0; for(start=0; start' || s == '=') { break; } } if (k>=n) { warning("match2_constraint: expr not present in this constraint","ignoring"); return; } /* printf("%d\n",k);*/ if (loc_expr(toks, k, start, &end) > 0) // (type = // not used { nitem = end + 1 - start; expr=make_expression(nitem,&toks[start]); /* printf("%d %d\n",i,j);*/ match2_cons_lhs[i][j]=expr; match2_cons_sign[i][j]=s; /* comm->par->parameters[pos]->type=4;*/ /* comm->par->parameters[pos]->expr=expr;*/ } else { warning("match2_constraint: no valid expression in constraint","ignoring"); } start=end+2; for (k = start; k < n; k++) { if (*toks[k] == ';') { break; } } if (loc_expr(toks, k, start, &end) > 0) // (type = // not used { nitem = end + 1 - start; expr=make_expression(nitem,&toks[start]); match2_cons_rhs[i][j]=expr; } else { match2_cons_lhs[i][j]=NULL; match2_cons_rhs[i][j]=NULL; warning("no valid expression in constraint","ignoring"); return; } match2_cons_weight[i][j]=command_par_value("weight",cmd->clone); for(start=0; startclone) ) == NULL) { /* printf("not-given-name\n");*/ cname=spec_join(&toks[start], nitem); } else { /* printf("given-name %s\n",cname);*/ }; match2_cons_name[i][j]=(char*) mymalloc("match2_constraint",strlen(cname)+1); /* strcpy(match2_cons_name[i][j],cname);*/ n=0; { int len = strlen(cname); for(k=0;k' && r > 0) fun_vec[k]=0; else if (s == '<' && r < 0) fun_vec[k]=0; match2_cons_value[i][j]=fun_vec[k]; match2_cons_value_rhs[i][j]=rhs; match2_cons_value_lhs[i][j]=lhs; k++; } return k; } int match2_print_var(struct in_cmd* cmd) { int n,l; char *varname,*knobfilename,*knobname; double ivalue,fvalue; FILE *knobfile=NULL; knobfilename=command_par_string("knobfile",cmd->clone); if (knobfilename){ knobfile=fopen(knobfilename,"w"); }; n=stored_match_var->curr; fprintf(prt_file, "\n\n"); fprintf(prt_file, "%-24s %-12s %-12s %-12s %-12s\n", "Variable", "Final Value", "Initial Value","Lower Limit", "Upper Limit"); for(l=0;l<80;l++) fprintf(prt_file, "-"); fprintf(prt_file,"\n"); for(l=0;lcommands[l]); ivalue=command_par_value("init",stored_match_var->commands[l]); fvalue=get_variable(varname); knobname=command_par_string("knob",stored_match_var->commands[l]); if (knobfilename){ fprintf(knobfile, "%-12s :=%+15.8e%+15.8e*%s;\n", varname,ivalue,fvalue-ivalue,knobname); } fprintf(prt_file,"%-24s",varname); fprintf(prt_file," %12.5e",fvalue); fprintf(prt_file," %12.5e",ivalue); fprintf(prt_file," %12.5e",command_par_value("lower",stored_match_var->commands[l])); fprintf(prt_file," %12.5e",command_par_value("upper",stored_match_var->commands[l])); fprintf(prt_file,"\n"); } fprintf(prt_file,"\n"); if ( knobfilename ) fclose(knobfile); return 0; }