////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///********* This program segment generates the flux vectors ****************///
///********* following certain inequality constraints ****************///
///********* and using random numbers ****************///
///********* of the RAND_MAX function. ****************///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include <stdlib.h>


/*Variable declaration*/
int i,j,k;
int noRows = 52, noColmns = 11; //Total no. of rows and columns of the file "nullspace.txt"
double **DATA;  //DATA[][] keeps the read data from the file
int L1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //Lower limit for flux
int U1[] = {9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,
9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,
9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999}; //Upper limit for flux


FILE *fpr,*fpw;

/*Function declaration*/
void read_data_file();
void mem_alloc();


/*Memory allocation for the basis vectors i.e. "nullspace.txt"*/
void mem_alloc(){
DATA=(double**)malloc(noRows*sizeof(double *));
 for(i=0;i<noRows;i++){
	DATA[i]=(double*)malloc(noColmns*sizeof(double));
  	if(DATA[i]==NULL)
   	printf("Memory allocation failed for DATA");
 }
}


/*Reading and storing of the data file "nullspace.txt" into the array*/
void read_data_file(){        
	fscanf(fpr,"%d %d", &noRows,&noColmns);
	mem_alloc();
	for(i=0;i<noRows;i++) 
		for(j=0;j<noColmns;j++)
  			fscanf(fpr,"%lf",&DATA[i][j]);
  			fscanf(fpr,"\n");
}


/*Main program for generation of the flux vectors in file "fluxvector.txt" through random numbers*/
int main()
{	
	int status =1, count=0, flag;
	double sum =0.0,r; //sum denotes the flux vector and r denotes the random number
	srand(564578); //function that generates random numbers
	fpr = fopen("nullspace.txt","r");
	fpw = fopen("fluxvector.txt","w");
	if(fpr==NULL)
       	{
       		fprintf(stderr,"Error:Cannot open read data file..\n");
       		exit(0);
       	}
       	if(fpw==NULL)
       	{
       		fprintf(stderr,"Error:Cannot open randnos file..\n");
       		exit(0);
       	}
       	read_data_file();
       	while(count<100)
       	{
       	for(i=0;i<noRows;i++) 
       		{
       			while(status)
       			{       
       				sum = 0.0;			
       				for(j=0;j<noColmns;j++)
       					{
       					r = ((double)rand()*100/RAND_MAX);
       					flag = (int)r%2;
       					if(flag)
       						r=r*(-1);
			 		sum+=DATA[i][j]*r;
	      		 		//printf("\nData = %lf ", DATA[i][j]);
			 		//getchar();			 		
			 		}
				if((sum>=L1[i]) && (sum<=U1[i]))
					{
					fprintf(fpw,"%lf",sum);
                             		break;
                               		}
				else       			
       					{
       					status=1;
       					printf("\nStaying at count %d,row = %d", count,i);
       					}
       			}
       		}
       		fprintf(fpw,"\n");
       		count++;
       	}
fclose(fpw);
fclose(fpr);
return(0);
}
