Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new add expression command #822

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile_test
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test-jacobian test-jacobian-2 test-jacobian-knobs \
test-match test-match-2 test-match-3 test-match-4 \
test-match-5 test-match-6 test-match-7 test-match-8 test-match-9 test-match-10\
test-aperture test-aperture-2 test-aperture-3 \
test-beam \
test-beam test-addexpression \
\
test-rfmultipole test-rfmultipole-2 test-rfmultipole-3 test-rfmultipole-4 test-rfmultipole-5 test-rfmultipole-6 test-rfmultipole-7 \
\
Expand Down
53 changes: 53 additions & 0 deletions src/mad_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ control(struct in_cmd* cmd)
// known control command in the same order as in mad_dict.c
if (strcmp(toks[k], "assign") == 0) exec_assign(cmd);
else if (strcmp(toks[k], "beam") == 0) exec_beam(cmd, 0);
else if (strcmp(toks[k], "add2expr") == 0) exec_add_expression(cmd);
else if (strcmp(toks[k], "beta0") == 0) store_beta0(cmd);
else if (strcmp(toks[k], "call") == 0) exec_call(cmd);
else if (strcmp(toks[k], "chdir") == 0) exec_chdir(cmd);
Expand Down Expand Up @@ -756,6 +757,58 @@ add_to_command_list_list(char* label, struct command_list* cl, struct command_li
}
}

void exec_add_expression(struct in_cmd* cmd){
char* varname = command_par_string_user("var", cmd->clone);
char* expchar = command_par_string_user("expr", cmd->clone);
if(expchar==NULL){
warning("Need to add an expression for: ", varname);
return;
}
struct variable* var;
struct expression* expr1, *expr2;
struct expression* exprcomb;

if ((var = find_variable(varname, variable_list)) != NULL){

if(var->type==2){
if(var->expr==NULL){

char *result = malloc(100 * sizeof(char));
sprintf(result, "%.16f", var->value);
expr1 = new_expression(result,NULL);
free(result);
var->expr = mymalloc("add expression", sizeof *var->expr);
}
else {
expr1 = clone_expression(var->expr);
}
expr2 = new_expression(expchar,NULL);
exprcomb = compound_expr(expr1, expression_value(expr1, 2), "+", expr2, expression_value(expr2, 2), 0);
memcpy (var->expr, exprcomb, sizeof(*exprcomb));
}
else if (var->type==1){

char *result = malloc(100 * sizeof(char));
sprintf(result, "%.16f", var->value);
expr1 = new_expression(result,NULL);
free(result);

expr2 = new_expression(expchar,NULL);
exprcomb = compound_expr(expr1, expression_value(expr1, 2), "+", expr2, expression_value(expr2, 2),0);
var->expr = mymalloc("add expression", sizeof *var->expr);
memcpy (var->expr, exprcomb, sizeof(*exprcomb));
var->type=2; //makes it an expression
}
else{
warning("Varialbe has to be declared as defered expression or as an assignment: ", varname);
}

}
else{
warning("The variable that trying to add expression to is not existing: ", varname);
}
}

void
dump_command(struct command* cmd)
{
Expand Down
1 change: 1 addition & 0 deletions src/mad_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ int make_line(char* statement);
int get_stmt(FILE* file, int supp_flag);
void get_defined_commands(char *);
void remove_from_command_list(char* label, struct command_list*);
void exec_add_expression(struct in_cmd* cmd);

#endif // MAD_CMD_H
4 changes: 4 additions & 0 deletions src/mad_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,10 @@ const char *const_command_def =
"row2 = [i, 0], "
"param = [s, interp]; " // change default t to interp
" "
"add2expr: control none 0 0 "
"var = [s, none], "
"expr = [s, none]; "
" "
"setvars_knob: control none 0 0 "
"table = [s, none], "
"knob = [s, none], "
Expand Down
30 changes: 21 additions & 9 deletions src/mad_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,41 +432,53 @@ scan_expr(int c_item, char** item) /* split input */
}

struct expression*
compound_expr(struct expression* e1, double v1, const char* oper, struct expression* e2, double v2)
compound_expr(struct expression* e1, double v1, const char* oper, struct expression* e2, double v2, int parentheses)
/* make one out of two expressions, using oper to connect them
hbu 9/2005 moved from madxn.c to makethin.c as only used here
and increased precision sprintf(tmp, "%e" -> sprintf(tmp, "%.14g" */
{
static char lb[] = "(", rb[] = ")";
char lb[] = "(", rb[] = ")";
char** toks = tmp_l_array->p;
struct expression* expr = NULL;
char tmp[30], op[30];
int n;
strcpy(op, oper);
if(parentheses== 0) { //In this way if no parenthesis are chosen they
lb[0] = (char) 0;
rb[0] = (char) 0;
if(e2->string[0]=='-') op[0] =' ';
}

if (e1 != NULL || e2 != NULL)
{
if (e1 != NULL)
{
if (e2 != NULL)
{
toks[0] = lb; toks[1] = e1->string; toks[2] = rb;
toks[0] = lb;
toks[1] = e1->string; toks[2] = rb;
toks[3] = op;
toks[4] = lb; toks[5] = e2->string; toks[6] = rb;
toks[4] = lb;
toks[5] = e2->string; toks[6] = rb;
}
else
{
sprintf(tmp, "%.14g", v2); /* hbu */
toks[0] = lb; toks[1] = e1->string; toks[2] = rb;
toks[0] = lb;
toks[1] = e1->string; toks[2] = rb;
toks[3] = op;
toks[4] = lb; toks[5] = tmp; toks[6] = rb;
toks[4] = lb;
toks[5] = tmp; toks[6] = rb;
}
}
else
{
sprintf(tmp, "%.14g", v1); /* hbu */
toks[0] = lb; toks[1] = tmp; toks[2] = rb;
toks[0] = lb;
toks[1] = tmp; toks[2] = rb;
toks[3] = op;
toks[4] = lb; toks[5] = e2->string; toks[6] = rb;
toks[4] = lb;
toks[5] = e2->string; toks[6] = rb;
}
join(toks, 7);
pre_split(c_join->c, l_wrk, 0);
Expand All @@ -480,7 +492,7 @@ compound_expr(struct expression* e1, double v1, const char* oper, struct express
struct expression*
scale_expr(struct expression* expr,double scale)
{
if (expr) return compound_expr(expr,0,"*",NULL,scale);
if (expr) return compound_expr(expr,0,"*",NULL,scale,1);
return NULL;
}

2 changes: 1 addition & 1 deletion src/mad_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct expression* new_expression(const char* in_string, struct int_array*);
struct expression* clone_expression(struct expression*);
struct expression* delete_expression(struct expression*);
struct expression* scale_expr(struct expression*, double scale);
struct expression* compound_expr(struct expression*, double v1, const char* oper, struct expression*, double v2);
struct expression* compound_expr(struct expression*, double v1, const char* oper, struct expression*, double v2, int parentheses);
double expr_combine(struct expression*, double v1, const char* oper, struct expression*, double v2, struct expression**);
double expression_value(struct expression*, int flag); /* recursive */
void dump_expression(struct expression*);
Expand Down