Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Jauslin <ian.jauslin@roma1.infn.it>2015-06-14 00:52:45 +0000
committerIan Jauslin <ian.jauslin@roma1.infn.it>2015-06-14 00:52:45 +0000
commitaa0f3ae2988d372b190b9bde2e75a6d17e744e93 (patch)
tree14482245c2fca27fcdad3078e97d0871352d52a7 /src/parse_file.c
Initial commitv1.2
Diffstat (limited to 'src/parse_file.c')
-rw-r--r--src/parse_file.c796
1 files changed, 796 insertions, 0 deletions
diff --git a/src/parse_file.c b/src/parse_file.c
new file mode 100644
index 0000000..6054372
--- /dev/null
+++ b/src/parse_file.c
@@ -0,0 +1,796 @@
+/*
+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 "parse_file.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "array.h"
+#include "fields.h"
+#include "rational.h"
+#include "number.h"
+#include "polynomial.h"
+#include "rcc.h"
+#include "definitions.cpp"
+#include "istring.h"
+#include "tools.h"
+#include "idtable.h"
+
+
+// parsing modes
+#define PP_NULL_MODE 0
+// when reading a factor
+#define PP_FACTOR_MODE 1
+// reading a monomial
+#define PP_MONOMIAL_MODE 2
+// reading a numerator and denominator
+#define PP_NUMBER_MODE 3
+// types of fields
+#define PP_FIELD_MODE 6
+#define PP_PARAMETER_MODE 7
+#define PP_EXTERNAL_MODE 8
+#define PP_INTERNAL_MODE 9
+#define PP_FERMIONS_MODE 10
+// indices
+#define PP_INDEX_MODE 11
+// factors or monomials
+#define PP_BRACKET_MODE 12
+// labels
+#define PP_LABEL_MODE 13
+// polynomial
+#define PP_POLYNOMIAL_MODE 14
+// group
+#define PP_GROUP_MODE 15
+
+
+// parse fields list
+int parse_input_fields(Char_Array str_fields, Fields_Table* fields){
+ // buffer
+ char* buffer=calloc(str_fields.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ int i,j;
+ int mode;
+ int comment=0;
+
+ // allocate memory
+ init_Fields_Table(fields);
+
+ // loop over input
+ mode=PP_NULL_MODE;
+ for(j=0;j<str_fields.length;j++){
+ if(comment==1){
+ if(str_fields.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ else{
+ switch(str_fields.str[j]){
+ // parameters
+ case 'h':
+ if(mode==PP_NULL_MODE){
+ mode=PP_PARAMETER_MODE;
+ }
+ break;
+ // external fields
+ case 'x':
+ if(mode==PP_NULL_MODE){
+ mode=PP_EXTERNAL_MODE;
+ }
+ break;
+ // internal fields
+ case 'i':
+ if(mode==PP_NULL_MODE){
+ mode=PP_INTERNAL_MODE;
+ }
+ break;
+ case 'f':
+ if(mode==PP_NULL_MODE){
+ mode=PP_FERMIONS_MODE;
+ }
+ break;
+
+ // reset buffer
+ case ':':
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ break;
+
+ // write to fields
+ case ',':
+ sscanf(buffer,"%d",&i);
+ if(mode==PP_PARAMETER_MODE){
+ int_array_append(i,&((*fields).parameter));
+ }
+ else if(mode==PP_EXTERNAL_MODE){
+ int_array_append(i,&((*fields).external));
+ }
+ else if(mode==PP_INTERNAL_MODE){
+ int_array_append(i,&((*fields).internal));
+ }
+ else if(mode==PP_FERMIONS_MODE){
+ int_array_append(i,&((*fields).fermions));
+ }
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ break;
+
+ // back to null mode
+ case '\n':
+ sscanf(buffer,"%d",&i);
+ if(mode==PP_PARAMETER_MODE){
+ int_array_append(i,&((*fields).parameter));
+ }
+ else if(mode==PP_EXTERNAL_MODE){
+ int_array_append(i,&((*fields).external));
+ }
+ else if(mode==PP_INTERNAL_MODE){
+ int_array_append(i,&((*fields).internal));
+ }
+ else if(mode==PP_FERMIONS_MODE){
+ int_array_append(i,&((*fields).fermions));
+ }
+ mode=PP_NULL_MODE;
+ break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ if(mode!=PP_NULL_MODE){
+ buffer_ptr=str_addchar(buffer_ptr,str_fields.str[j]);
+ }
+ break;
+ }
+ }
+ }
+ free(buffer);
+ return(0);
+}
+
+
+// parse symbols list
+// write result to fields
+int parse_input_symbols(Char_Array str_symbols, Fields_Table* fields){
+ // buffer
+ char* buffer=calloc(str_symbols.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ Polynomial polynomial;
+ int index;
+ int i,j;
+ int mode;
+ int comment=0;
+
+ // loop over input
+ mode=PP_INDEX_MODE;
+ for(j=0;j<str_symbols.length;j++){
+ if(comment==1){
+ if(str_symbols.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ // stay in polynomial mode until ','
+ else if(mode==PP_POLYNOMIAL_MODE){
+ if(str_symbols.str[j]==','){
+ // parse polynomial
+ str_to_Polynomial(buffer, &polynomial);
+ // write index and polynomial
+ symbols_append_noinit(index, polynomial, &((*fields).symbols));
+ mode=PP_INDEX_MODE;
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ else{
+ buffer_ptr=str_addchar(buffer_ptr,str_symbols.str[j]);
+ }
+ }
+ else{
+ switch(str_symbols.str[j]){
+ // polynomial mode
+ case '=':
+ if(mode==PP_INDEX_MODE){
+ // read index
+ sscanf(buffer,"%d",&index);
+ // reset buffer
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ mode=PP_POLYNOMIAL_MODE;
+ }
+ break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ buffer_ptr=str_addchar(buffer_ptr,str_symbols.str[j]);
+ break;
+ }
+ }
+ }
+
+ // last step
+ if(polynomial.length>0){
+ str_to_Polynomial(buffer, &polynomial);
+ symbols_append_noinit(index, polynomial, &((*fields).symbols));
+ }
+
+ // simplify
+ for(i=0;i<(*fields).symbols.length;i++){
+ polynomial_simplify((*fields).symbols.expr+i, *fields);
+ }
+
+ free(buffer);
+ return(0);
+}
+
+// parse groups of independent fields
+int parse_input_groups(Char_Array str_groups, Groups* groups){
+ // buffer
+ char* buffer=calloc(str_groups.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ int index;
+ int j;
+ Int_Array group;
+ int mode;
+ int comment=0;
+
+ // alloc
+ init_Groups(groups, GROUP_SIZE);
+
+ // loop over input
+ mode=PP_NULL_MODE;
+ for(j=0;j<str_groups.length;j++){
+ if(comment==1){
+ if(str_groups.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ else{
+ switch(str_groups.str[j]){
+ // group mode
+ case '(':
+ if(mode==PP_NULL_MODE){
+ // reset buffer
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ // init
+ init_Int_Array(&group, GROUP_SIZE);
+ mode=PP_GROUP_MODE;
+ }
+ break;
+ case')':
+ if(mode==PP_GROUP_MODE){
+ sscanf(buffer,"%d",&index);
+ int_array_append(index, &group);
+ groups_append_noinit(group, groups);
+ mode=PP_NULL_MODE;
+ }
+ break;
+
+ // read index
+ case',':
+ if(mode==PP_GROUP_MODE){
+ sscanf(buffer,"%d",&index);
+ int_array_append(index, &group);
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ if(mode!=PP_NULL_MODE){
+ buffer_ptr=str_addchar(buffer_ptr,str_groups.str[j]);
+ }
+ break;
+ }
+ }
+ }
+
+ free(buffer);
+ return(0);
+}
+
+
+// parse identities between fields
+// write result to fields
+int parse_input_identities(Char_Array str_identities, Fields_Table* fields){
+ // buffer
+ char* buffer=calloc(str_identities.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ Int_Array monomial;
+ Polynomial polynomial;
+ int i,j;
+ int sign;
+ int tmp;
+ int mode;
+ int comment=0;
+
+ init_Int_Array(&monomial, MONOMIAL_SIZE);
+
+ // loop over input
+ mode=PP_NULL_MODE;
+ for(j=0;j<str_identities.length;j++){
+ if(comment==1){
+ if(str_identities.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ // stay in polynomial mode until ','
+ else if(mode==PP_POLYNOMIAL_MODE){
+ if(str_identities.str[j]==','){
+ // parse polynomial
+ str_to_Polynomial(buffer, &polynomial);
+ // write monomial and polynomial
+ identities_append_noinit(monomial, polynomial, &((*fields).ids));
+ // realloc
+ init_Int_Array(&monomial, MONOMIAL_SIZE);
+ mode=PP_NULL_MODE;
+ }
+ else{
+ buffer_ptr=str_addchar(buffer_ptr,str_identities.str[j]);
+ }
+ }
+ else{
+ switch(str_identities.str[j]){
+ // monomial
+ case '[':
+ if(mode==PP_NULL_MODE){
+ mode=PP_INDEX_MODE;
+ }
+ if(mode==PP_INDEX_MODE){
+ mode=PP_BRACKET_MODE;
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ break;
+ // for notational homogeneity
+ case 'f':
+ if(mode==PP_BRACKET_MODE){
+ mode=PP_FIELD_MODE;
+ }
+ break;
+ // write monomial term
+ case ']':
+ if(mode==PP_FIELD_MODE){
+ sscanf(buffer,"%d",&i);
+ int_array_append(i,&monomial);
+ mode=PP_INDEX_MODE;
+ }
+ break;
+
+ // polynomial mode
+ case '=':
+ if(mode==PP_INDEX_MODE){
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ mode=PP_POLYNOMIAL_MODE;
+ }
+ break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ if(mode!=PP_NULL_MODE){
+ buffer_ptr=str_addchar(buffer_ptr,str_identities.str[j]);
+ }
+ break;
+ }
+ }
+ }
+
+ // last step
+ if(mode==PP_POLYNOMIAL_MODE){
+ str_to_Polynomial(buffer, &polynomial);
+ identities_append_noinit(monomial, polynomial, &((*fields).ids));
+ }
+ else{
+ free_Int_Array(monomial);
+ }
+
+ // sort
+ // don't use the identities to simplify
+ tmp=(*fields).ids.length;
+ (*fields).ids.length=0;
+ for(i=0;i<tmp;i++){
+ sign=1;
+ monomial_sort((*fields).ids.lhs[i], 0, (*fields).ids.lhs[i].length-1, *fields, &sign);
+ polynomial_simplify((*fields).ids.rhs+i, *fields);
+ polynomial_multiply_Qscalar((*fields).ids.rhs[i],quot(sign,1));
+ }
+ (*fields).ids.length=tmp;
+
+ free(buffer);
+ return(0);
+}
+
+// parse propagator
+int parse_input_propagator(Char_Array str_propagator, Polynomial_Matrix* propagator, Fields_Table fields){
+ // buffer
+ char* buffer=calloc(str_propagator.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ int i,j;
+ int index1=-1;
+ int index2=-1;
+ int mode;
+ int comment=0;
+
+ // allocate memory
+ init_Polynomial_Matrix(propagator, fields.internal.length);
+
+ // copy indices
+ for(i=0;i<fields.internal.length;i++){
+ (*propagator).indices[i]=fields.internal.values[i];
+ }
+
+ // loop over input
+ mode=PP_INDEX_MODE;
+ for(j=0;j<str_propagator.length;j++){
+ if(comment==1){
+ if(str_propagator.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ else{
+ switch(str_propagator.str[j]){
+ // indices
+ case ';':
+ if(mode==PP_INDEX_MODE){
+ sscanf(buffer,"%d",&i);
+ index1=intlist_find_err((*propagator).indices, (*propagator).length, i);
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ break;
+ case ':':
+ if(mode==PP_INDEX_MODE){
+ sscanf(buffer,"%d",&i);
+ index2=intlist_find_err((*propagator).indices, (*propagator).length, i);
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ mode=PP_POLYNOMIAL_MODE;
+ }
+ break;
+
+ // num
+ case ',':
+ if(mode==PP_POLYNOMIAL_MODE && index1>=0 && index2>=0){
+ free_Polynomial((*propagator).matrix[index1][index2]);
+ str_to_Polynomial(buffer,(*propagator).matrix[index1]+index2);
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ mode=PP_INDEX_MODE;
+ }
+ break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ buffer_ptr=str_addchar(buffer_ptr,str_propagator.str[j]);
+ break;
+ }
+ }
+ }
+
+ // last step
+ if(mode==PP_POLYNOMIAL_MODE){
+ free_Polynomial((*propagator).matrix[index1][index2]);
+ str_to_Polynomial(buffer,(*propagator).matrix[index1]+index2);
+ }
+
+ free(buffer);
+ return(0);
+}
+
+
+// parse input polynomial
+int parse_input_polynomial(Char_Array str_polynomial, Polynomial* output, Fields_Table fields){
+ int j;
+ // buffer
+ char* buffer=calloc(str_polynomial.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ Polynomial tmp_poly;
+
+ // allocate memory
+ init_Polynomial(output,POLY_SIZE);
+
+ for(j=0;j<str_polynomial.length;j++){
+ switch(str_polynomial.str[j]){
+ case '*':
+ str_to_Polynomial(buffer, &tmp_poly);
+ if((*output).length==0){
+ polynomial_concat(tmp_poly, output);
+ }
+ else{
+ polynomial_prod_chain(tmp_poly, output, fields);
+ }
+ free_Polynomial(tmp_poly);
+
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ break;
+
+ default:
+ buffer_ptr=str_addchar(buffer_ptr,str_polynomial.str[j]);
+ break;
+ }
+ }
+
+ //last step
+ str_to_Polynomial(buffer, &tmp_poly);
+ if((*output).length==0){
+ polynomial_concat(tmp_poly, output);
+ }
+ else{
+ polynomial_prod_chain(tmp_poly, output, fields);
+ }
+ free_Polynomial(tmp_poly);
+
+ free(buffer);
+ return(0);
+}
+
+
+
+// parse id table
+// fields argument for sorting
+int parse_input_id_table(Char_Array str_idtable, Id_Table* idtable, Fields_Table fields){
+ // buffer
+ char* buffer=calloc(str_idtable.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ int index;
+ Polynomial polynomial;
+ int j;
+ int mode;
+ int comment=0;
+
+ // allocate memory
+ init_Id_Table(idtable,EQUATION_SIZE);
+
+ // loop over input
+ mode=PP_INDEX_MODE;
+ for(j=0;j<str_idtable.length;j++){
+ if(comment==1){
+ if(str_idtable.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ else{
+ switch(str_idtable.str[j]){
+ // end polynomial mode
+ case ',':
+ // write polynomial
+ if(mode==PP_POLYNOMIAL_MODE){
+ str_to_Polynomial(buffer,&polynomial);
+ // add to idtable
+ idtable_append_noinit(index,polynomial,idtable);
+ mode=PP_INDEX_MODE;
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ break;
+
+ case ':':
+ if(mode==PP_INDEX_MODE){
+ sscanf(buffer,"%d",&index);
+ mode=PP_POLYNOMIAL_MODE;
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ if(mode!=PP_NULL_MODE){
+ buffer_ptr=str_addchar(buffer_ptr,str_idtable.str[j]);
+ }
+ break;
+ }
+ }
+ }
+
+ //last step
+ if(mode==PP_POLYNOMIAL_MODE){
+ str_to_Polynomial(buffer,&polynomial);
+ idtable_append_noinit(index,polynomial,idtable);
+ }
+
+ // sort
+ for(j=0;j<(*idtable).length;j++){
+ polynomial_simplify((*idtable).polynomials+j, fields);
+ }
+
+ free(buffer);
+ return(0);
+}
+
+// parse a list of labels
+int parse_labels(Char_Array str_labels, Labels* labels){
+ // buffer
+ char* buffer=calloc(str_labels.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ Char_Array label;
+ int index;
+ int j;
+ int mode;
+ int comment=0;
+
+ // allocate memory
+ init_Labels(labels,EQUATION_SIZE);
+
+ // loop over input
+ mode=PP_INDEX_MODE;
+ for(j=0;j<str_labels.length;j++){
+ if(comment==1){
+ if(str_labels.str[j]=='\n'){
+ comment=0;
+ }
+ }
+ else{
+ switch(str_labels.str[j]){
+ case '"':
+ if(mode==PP_INDEX_MODE){
+ mode=PP_LABEL_MODE;
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ // write
+ else if(mode==PP_LABEL_MODE){
+ str_to_char_array(buffer,&label);
+ labels_append_noinit(label,index,labels);
+ mode=PP_INDEX_MODE;
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ }
+ break;
+
+ case ':':
+ // write
+ if(mode==PP_INDEX_MODE){
+ sscanf(buffer,"%d",&index);
+ }
+ break;
+
+ // characters to ignore
+ case ' ':break;
+ case '&':break;
+ case '\n':break;
+ case ',':break;
+
+ // comment
+ case '#':
+ comment=1;
+ break;
+
+ default:
+ buffer_ptr=str_addchar(buffer_ptr,str_labels.str[j]);
+ break;
+ }
+ }
+ }
+
+ free(buffer);
+ return(0);
+}
+
+
+// read initial condition for numerical computation
+int parse_init_cd(Char_Array init_cd, RCC* init){
+ char* buffer=calloc(init_cd.length+1,sizeof(char));
+ char* buffer_ptr=buffer;
+ int index=0;
+ int i,j;
+ int comment_mode=0;
+ int dcount=0;
+
+ *buffer_ptr='\0';
+ // loop over the input
+ for(j=0;j<init_cd.length;j++){
+ if(comment_mode==1){
+ if(init_cd.str[j]=='\n'){
+ comment_mode=0;
+ }
+ }
+ else{
+ switch(init_cd.str[j]){
+ // new term
+ case ',':
+ // write init
+ sscanf(buffer,"%Lf",(*init).values+intlist_find_err((*init).indices,(*init).length,index));
+ // reset buffer
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ // reset derivatives counter
+ dcount=0;
+ break;
+ // separator
+ case ':':
+ // write index
+ sscanf(buffer,"%d",&i);
+ if(i<0){
+ index=i-dcount*DOFFSET;
+ }
+ else{
+ index=i+dcount*DOFFSET;
+ }
+ buffer_ptr=buffer;
+ *buffer_ptr='\0';
+ break;
+ // derivatives
+ case 'd':
+ dcount++;
+ break;
+
+ // characters to ignore
+ case ' ':break;
+ case '&':break;
+ case '\n':break;
+
+ // comments
+ case '#':
+ comment_mode=1;
+ break;
+
+ default:
+ // write to buffer
+ buffer_ptr=str_addchar(buffer_ptr,init_cd.str[j]);
+ break;
+ }
+ }
+ }
+
+ // write init
+ sscanf(buffer,"%Lf",(*init).values+unlist_find((*init).indices,(*init).length,index));
+
+ free(buffer);
+ return(0);
+}
+
+
+// set indices and length of init
+int prepare_init(int* indices, int length, RCC* init){
+ int i;
+ init_RCC(init, length);
+ for(i=0;i<length;i++){
+ (*init).indices[i]=indices[i];
+ // set constants to 1
+ if(indices[i]<0 && indices[i]>-DOFFSET){
+ (*init).values[i]=1.;
+ }
+ else{
+ (*init).values[i]=0.;
+ }
+
+ }
+ return(0);
+}