Ian Jauslin
summaryrefslogtreecommitdiff
blob: dce7de6335d63b7d4857634fc324eac95bbed1d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
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.
*/

/*
numkondo

Compute the flow of a flow equation numerically

*/


#include <stdio.h>
#include <stdlib.h>

// pre-compiler definitions
#include "definitions.cpp"

// rccs
#include "rcc.h"
// grouped representation of polynomials
#include "grouped_polynomial.h"
// command line parser
#include "cli_parser.h"
// parse input file
#include "parse_file.h"
// numerical flow
#include "flow.h"
// arrays
#include "array.h"

// read cli arguments
int read_args_numkondo(int argc,const char* argv[], Str_Array* str_args, Numkondo_Options* opts);
// print usage message
int print_usage_numkondo();
// compute flow
int numflow(Str_Array str_args, Numkondo_Options opts);


int main (int argc, const char* argv[]){
  // string arguments
  Str_Array str_args;
  // options
  Numkondo_Options opts;

  // read command-line arguments
  read_args_numkondo(argc,argv,&str_args,&opts);

  numflow(str_args, opts);

  //free memory
  free_Str_Array(str_args);
  return(0);
}


// parse command-line arguments
#define CP_FLAG_NITER 1
#define CP_FLAG_TOL 2
#define CP_FLAG_RCCS 3
int read_args_numkondo(int argc,const char* argv[], Str_Array* str_args, Numkondo_Options* opts){
  int i;
  // pointers
  char* ptr;
  // file to read the polynomial from in flow mode
  const char* file="";
  // flag that indicates what argument is being read
  int flag=0;
  // whether a file was specified on the command-line
  int exists_file=0;

  // if there are no arguments
  if(argc==1){
    print_usage_numkondo();
    exit(-1);
  }

  // defaults
  // display entire flow
  (*opts).display_mode=DISPLAY_NUMERICAL;
  // default niter
  (*opts).niter=100;
  // default to 0 tolerance
  (*opts).tol=0;
  // 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=1;i<argc;i++){
    // flag
    if(argv[i][0]=='-'){
      for(ptr=((char*)argv[i])+1;*ptr!='\0';ptr++){
	switch(*ptr){
	// final step: display the final step of the integration with maximal precision
	case 'F':
	  (*opts).display_mode=DISPLAY_FINAL;
	  break;
	// niter
	case 'N':
	  flag=CP_FLAG_NITER;
	  break;
	// tolerance
	case 'D':
	  flag=CP_FLAG_TOL;
	  break;
	// initial condition
	case 'I':
	  flag=CP_FLAG_RCCS;
	  break;
	// print version
	case 'v':
	  printf("numkondo " VERSION "\n");
	  exit(1);
	  break;
	}
      }
    }
    // if the niter flag is up
    else if (flag==CP_FLAG_NITER){
      // read niter
      sscanf(argv[i],"%d",&((*opts).niter));
      // reset flag
      flag=0;
    }
    // tolerance
    else if (flag==CP_FLAG_TOL){
      sscanf(argv[i],"%Lf",&((*opts).tol));
      flag=0;
    }
    // init condition
    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);
}

// print usage message
int print_usage_numkondo(){
  printf("\nusage:\n   numkondo [-F] [-N niter] [-D tolerance] [-I initial_condition] <filename>\n\n");
  return(0);
}


// numerical computation of the flow
int numflow(Str_Array str_args, Numkondo_Options opts){
  // index of the entry in the input file
  int arg_index;
  // list of rccs
  Labels labels;
  // initial condition
  RCC init_cd;
  // flow equation
  Grouped_Polynomial flow_equation;

  // parse id table
  arg_index=find_str_arg("labels", str_args);
  if(arg_index<0){
    fprintf(stderr,"error: no labels entry in the configuration file\n");
    exit(-1);
  }
  else{
    parse_labels(str_args.strs[arg_index], &labels);
  }

  // parse flow equation
  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);
  }

  // initial conditions
  // 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){
      fprintf(stderr,"error: no initial condition in the configuration file or on the command line\n");
      exit(-1);
    }
    else{
      char_array_cpy(str_args.strs[arg_index],&(opts.eval_rccstring));
    }
  }
  // initialize the rccs
  prepare_init(flow_equation.indices,flow_equation.length,&init_cd);
  // read rccs from string
  if(opts.eval_rccstring.length!=-1){
    parse_init_cd(opts.eval_rccstring, &init_cd);
    free_Char_Array(opts.eval_rccstring);
  }

  numerical_flow(flow_equation, init_cd, labels, opts.niter, opts.tol, opts.display_mode);

  free_RCC(init_cd);

  // free memory
  free_Labels(labels);
  free_Grouped_Polynomial(flow_equation);
  return(0);
}