diff options
Diffstat (limited to 'src/meantools_eval.c')
-rw-r--r-- | src/meantools_eval.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/meantools_eval.c b/src/meantools_eval.c new file mode 100644 index 0000000..50fff5b --- /dev/null +++ b/src/meantools_eval.c @@ -0,0 +1,129 @@ +/* +Copyright 2015 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "meantools_eval.h" + +#include <stdio.h> +#include <stdlib.h> +#include "parse_file.h" +#include "cli_parser.h" +#include "grouped_polynomial.h" +#include "array.h" +#include "rcc.h" + + +#define CP_FLAG_RCCS 1 +// read command line arguments +int tool_eval_read_args(int argc, const char* argv[], Str_Array* str_args, Meantools_Options* opts){ + // file to read the polynomial from in flow mode + const char* file=""; + // whether a file was specified on the command-line + int exists_file=0; + // flag + int flag=0; + int i; + char* ptr; + + // defaults + // mark rccstring so that it can be recognized whether it has been set or not + (*opts).eval_rccstring.length=-1; + + // loop over arguments + for(i=2;i<argc;i++){ + // flag + if(argv[i][0]=='-'){ + for(ptr=((char*)argv[i])+1;*ptr!='\0';ptr++){ + switch(*ptr){ + // evaluate string + case 'R': + flag=CP_FLAG_RCCS; + break; + } + } + } + // rccs + else if(flag==CP_FLAG_RCCS){ + str_to_char_array((char*)argv[i], &((*opts).eval_rccstring)); + flag=0; + } + // read file name from command-line + else{ + file=argv[i]; + exists_file=1; + } + } + + read_config_file(str_args, file, 1-exists_file); + + return(0); +} + + +// evaluate a flow equation on a vector of rccs +int tool_eval(Str_Array str_args, Meantools_Options opts){ + // index of the entry in the input file + int arg_index; + // rccs + RCC rccs; + // flow equation + Grouped_Polynomial flow_equation; + + + // parse flow equation + // if there is a unique argument, assume it is the flow equation + if(str_args.length==1){ + char_array_to_Grouped_Polynomial(str_args.strs[0], &flow_equation); + } + else{ + arg_index=find_str_arg("flow_equation", str_args); + if(arg_index<0){ + fprintf(stderr,"error: no flow equation entry in the configuration file\n"); + exit(-1); + } + else{ + char_array_to_Grouped_Polynomial(str_args.strs[arg_index], &flow_equation); + } + + // rccs + // check they were not specified on the command line + if(opts.eval_rccstring.length==-1){ + arg_index=find_str_arg("initial_condition", str_args); + if(arg_index>=0){ + char_array_cpy(str_args.strs[arg_index],&(opts.eval_rccstring)); + } + } + } + + // initialize the rccs + prepare_init(flow_equation.indices,flow_equation.length,&rccs); + // read rccs from string + if(opts.eval_rccstring.length!=-1){ + parse_init_cd(opts.eval_rccstring, &rccs); + free_Char_Array(opts.eval_rccstring); + } + + // evaluate + evaleq(&rccs, flow_equation); + + // print + RCC_print(rccs); + + // free memory + free_Grouped_Polynomial(flow_equation); + free_RCC(rccs); + return(0); +} + |