Ian Jauslin
summaryrefslogtreecommitdiff
blob: 8b5c19775397c51dc3747d536f9a8e40f933d2cd (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Copyright 2015-2022 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.
*/

/*
meankondo

A tool to compute the renormalization group flow for Fermionic hierarchical models

*/


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

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

// various arrays
#include "array.h"

// list of fields
#include "fields.h"
// numbers
#include "number.h"
// polynomials
#include "polynomial.h"
// list of rccs
#include "idtable.h"
// grouped representation of polynomials
#include "grouped_polynomial.h"
// command line parser
#include "cli_parser.h"
// parse input file
#include "parse_file.h"
// means
#include "mean.h"
// various string operations
#include "istring.h"
// symbolic trees
# include "tree.h"

// read cli arguments
int read_args_meankondo(int argc,const char* argv[], Str_Array* str_args, Meankondo_Options* opts);
// print usage message
int print_usage_meankondo();
// check consistency of options
int check_meankondo_opts(Meankondo_Options opts);
// compute flow
int compute_flow(Str_Array str_args, Meankondo_Options opts);
// compute average
int compute_average(Polynomial init_poly, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, int threads, int print_progress, Polynomial* exp_poly);


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

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

  // check command-line arguments
  check_meankondo_opts(opts);

  // warning message if representing rational numbers as floats
#ifdef RATIONAL_AS_FLOAT
  fprintf(stderr,"info: representing rational numbers using floats\n");
#endif

  compute_flow(str_args, opts);

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


// parse command-line arguments
#define CP_FLAG_THREADS 1
int read_args_meankondo(int argc,const char* argv[], Str_Array* str_args, Meankondo_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;


  // defaults
  // single thread
  (*opts).threads=1;
  // do not chain
  (*opts).chain=0;
  // do not print progress
  (*opts).print_progress=0;
  // print the flow equation
  (*opts).group_poly=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){
	// threads
	case 't':
	  flag=CP_FLAG_THREADS;
	  break;
	// chain
	case 'C':
	  (*opts).chain=1;
	  break;
	// print progress
	case 'p':
	  (*opts).print_progress=1;
	  break;
	case 'A':
	  (*opts).group_poly=0;
	  break;
	// print version
	case 'v':
	  printf("meankondo " VERSION "\n");
	  exit(1);
	  break;
	default:
	  print_usage_meankondo();
	  exit(-1);
	  break;
	}
      }
    }
    // threads
    else if(flag==CP_FLAG_THREADS){
      sscanf(argv[i],"%d",&((*opts).threads));
      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_meankondo(){
  printf("\nusage:\n   meankondo [-t threads] [-C] [-p] [-A] <filename>\n\n");
  return(0);
}

// check consistency of options
int check_meankondo_opts(Meankondo_Options opts){
  if(opts.chain==1 && opts.group_poly==0){
    fprintf(stderr,"aborting: the '-C' and '-A' options are incompatible\n");
    exit(-1);
  }
  return(0);
}


// compute the renormalization group flow
int compute_flow(Str_Array str_args, Meankondo_Options opts){
  int i;
  // index of the entry in the input file
  int arg_index;
  // header of the entry
  Char_Array arg_header;
  // list of fields
  Fields_Table fields;
  // their propagator
  Polynomial_Matrix propagator;
  // preprocessor variables
  Variables variables;
  // initial polynomial
  Polynomial init_poly;
  // list of rccs
  Id_Table idtable;
  // groups of independent fields
  Groups groups;
  // flow equation
  Grouped_Polynomial flow_equation;
  // polynomial produced by the averaging operation
  Polynomial exp_poly;


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

  // parse variables
  // must precede id_table, virtual_fields, identities and input_polynomial
  arg_index=find_str_arg("preprocessor_variables", str_args);
  if(arg_index>=0){
    parse_input_variables(str_args.strs[arg_index],&variables);
  }
  else{
    init_Variables(&variables,1);
  }

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

  // parse virtual_fields
  arg_index=find_str_arg("virtual_fields", str_args);
  if(arg_index>=0){
    parse_input_virtual_fields(str_args.strs[arg_index], &fields, variables);
  }

  // parse input polynomial
  arg_index=find_str_arg("input_polynomial", str_args);
  if(arg_index>=0){
    parse_input_polynomial(str_args.strs[arg_index],&init_poly, fields, variables);
  }
  else{
    fprintf(stderr,"error: no input polynomial entry in the configuration file\n");
    exit(-1);
  }

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

  // parse identities
  arg_index=find_str_arg("identities", str_args);
  if(arg_index>=0){
    parse_input_identities(str_args.strs[arg_index],&fields, variables);
  }

  // parse groups (must come after virtual_fields and propagator)
  arg_index=find_str_arg("groups", str_args);
  if(arg_index>=0){
    parse_input_groups(str_args.strs[arg_index],&groups, propagator, fields);
  }
  else{
    init_Groups(&groups, 1);
  }

  // compute the average
  compute_average(init_poly, fields, propagator, groups, opts.threads, opts.print_progress, &exp_poly);

  free_Polynomial(init_poly);
  free_Polynomial_Matrix(propagator);
  free_Groups(groups);

  // parse postprocessing entry
  arg_index=find_str_arg("postprocess_operation", str_args);
  if(arg_index>=0){
    add_polynomial_to_variables("OUT", exp_poly, &variables);
    // parse postprocess entry
    Polynomial postprocess_operation;
    parse_input_polynomial(str_args.strs[arg_index], &postprocess_operation, fields, variables);
    // replace exp_poly
    free_Polynomial(exp_poly);
    exp_poly=postprocess_operation;
  }

  if(opts.group_poly==1){
    // flow equation
    group_polynomial(exp_poly, &flow_equation, idtable, fields);
  }

  // postprocess flow equation
  arg_index=find_str_arg("postprocess_flow_equation", str_args);
  if(arg_index>=0){
    Polynomial flow_polynomial;
    // polynomial made of the rcc's multiplied by the corresponding fields (parsed from idtable)
    idtable_to_polynomial(idtable, &flow_polynomial);

    // add to variables
    add_polynomial_to_variables("FLOW", flow_polynomial, &variables);
    free_Polynomial(flow_polynomial);

    // parse postprocess entry
    Polynomial postprocess_polynomial;
    parse_input_polynomial(str_args.strs[arg_index], &postprocess_polynomial, fields, variables);

    // convert to flow equation
    Grouped_Polynomial postprocess_flow_equation;
    group_polynomial(postprocess_polynomial, &postprocess_flow_equation, idtable, fields);
    free_Polynomial(postprocess_polynomial);

    // apply postprocessing to flow equation
    Grouped_Polynomial new_flow;
    compose_flow_equations(postprocess_flow_equation, flow_equation, &new_flow);
    free_Grouped_Polynomial(postprocess_flow_equation);

    // replace flow_equation
    free_Grouped_Polynomial(flow_equation);
    flow_equation=new_flow;
  }


  // if chain then print config file
  if(opts.chain==1){
    for(i=0;i<str_args.length;i++){
      // check whether to print the str_arg
      get_str_arg_title(str_args.strs[i], &arg_header);
      if (\
	str_cmp(arg_header.str, "variables")==0 &&\
	str_cmp(arg_header.str, "virtual_fields")==0 &&\
	str_cmp(arg_header.str, "groups")==0 &&\
	str_cmp(arg_header.str, "fields")==0 &&\
	str_cmp(arg_header.str, "identities")==0 &&\
	str_cmp(arg_header.str, "propagator")==0 &&\
	str_cmp(arg_header.str, "input_polynomial")==0 &&\
	str_cmp(arg_header.str, "id_table")==0 &&\
	str_cmp(arg_header.str, "postprocess_operation")==0 &&\
	str_cmp(arg_header.str, "numerical_postprocess_operation")==0
	){
	  printf("%s\n&\n",str_args.strs[i].str);
      }
      free_Char_Array(arg_header);
    }
    // print flow equation
    printf("#!flow_equation\n");
  }

  // print result
  if(opts.group_poly==1){
    grouped_polynomial_print(flow_equation,'%','%');

    // free memory
    free_Grouped_Polynomial(flow_equation);
  }
  else{
    polynomial_print(exp_poly);
  }

  // free memory
  free_Polynomial(exp_poly);

  // parse numerical_postprocessing entry
  arg_index=find_str_arg("numerical_postprocess_operation", str_args);
  if(arg_index>=0){
    Polynomial rcc_polynomial;
    // polynomial made of the rcc's multiplied by the corresponding fields (parsed from idtable)
    idtable_to_polynomial(idtable, &rcc_polynomial);

    // add to variables
    add_polynomial_to_variables("RCC", rcc_polynomial, &variables);
    free_Polynomial(rcc_polynomial);

    // parse postprocess entry
    Polynomial numerical_postprocess_operation;
    parse_input_polynomial(str_args.strs[arg_index], &numerical_postprocess_operation, fields, variables);

    // convert to flow equation
    Grouped_Polynomial numerical_postprocess_flow_equation;
    group_polynomial(numerical_postprocess_operation, &numerical_postprocess_flow_equation, idtable, fields);
    free_Polynomial(numerical_postprocess_operation);

    // print postprocessing flow equation
    printf("\n&\n#!postprocess_operation\n");
    grouped_polynomial_print(numerical_postprocess_flow_equation,'%','%');
    free_Grouped_Polynomial(numerical_postprocess_flow_equation);
  }

  if(opts.group_poly==1){
    free_Id_Table(idtable);
  }
  free_Fields_Table(fields);
  free_Variables(variables);

  return(0);
}


// compute average
int compute_average(Polynomial init_poly, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, int threads, int print_progress, Polynomial* exp_poly){
  polynomial_cpy(init_poly,exp_poly);

  // average
  if(threads>1){
    polynomial_mean_multithread(exp_poly, fields, propagator, groups, threads, print_progress);
  }
  else{
    polynomial_mean(exp_poly, fields, propagator, groups, print_progress);
  }
  return(0);
}