Ian Jauslin
summaryrefslogtreecommitdiff
blob: 1c976d465f71efbbd3cd5c326e4d396b34a8be70 (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
/*
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.
*/

/*
meankondo

A simple 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"

// 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();
// compute flow
int compute_flow(Str_Array str_args, Meankondo_Options opts);
// compute the flow equation
int compute_flow_equation(Polynomial init_poly, Id_Table idtable, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, int threads, Grouped_Polynomial* flow_equation);


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);

  // 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;

  // 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 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] <filename>\n\n");
  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;
  // initial polynomial
  Polynomial init_poly;
  // list of rccs
  Id_Table idtable;
  // groups of independent fields
  Groups groups;
  // flow equation
  Grouped_Polynomial flow_equation;


  // 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 id table
  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);
  }

  // parse symbols
  arg_index=find_str_arg("symbols", str_args);
  if(arg_index>=0){
    parse_input_symbols(str_args.strs[arg_index],&fields);
  }
  else{
    init_Symbols(&(fields.symbols),1);
  }

  // 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);
  }
  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);
  }
  else{
    init_Identities(&(fields.ids),1);
  }

  // parse groups
  arg_index=find_str_arg("groups", str_args);
  if(arg_index>=0){
    parse_input_groups(str_args.strs[arg_index],&groups);
  }
  else{
    init_Groups(&groups, 1);
  }

  // flow equation
  compute_flow_equation(init_poly, idtable, fields, propagator, groups, opts.threads, &flow_equation);
  free_Polynomial(init_poly);
  free_Polynomial_Matrix(propagator);
  free_Fields_Table(fields);
  free_Groups(groups);

  // 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, "symbols")==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 ){
	  printf("%s\n&\n",str_args.strs[i].str);
      }
      free_Char_Array(arg_header);
    }
    // print flow equation
    printf("#!flow_equation\n");
  }

  // print flow equation
  grouped_polynomial_print(flow_equation,'%','%');

  // free memory
  free_Id_Table(idtable);
  free_Grouped_Polynomial(flow_equation);
  return(0);
}


// compute the flow equation
int compute_flow_equation(Polynomial init_poly, Id_Table idtable, Fields_Table fields, Polynomial_Matrix propagator, Groups groups, int threads, Grouped_Polynomial* flow_equation){
  // expectation
  Polynomial exp_poly;

  polynomial_cpy(init_poly,&exp_poly);

  // average
  if(threads>1){
    polynomial_mean_multithread(&exp_poly, fields, propagator, groups, threads);
  }
  else{
    polynomial_mean(&exp_poly, fields, propagator, groups);
  }
    
  // grouped representation of expanded_poly
  group_polynomial(exp_poly,flow_equation,idtable, fields);
  free_Polynomial(exp_poly);

  return(0);
}