Esempio n. 1
0
int main(void)
{
	long idum=(-3);
	unsigned long err,i,j,k,nn1=NX,nn2=NY,nn3=NZ;
	float fnorm,***data1,***data2,**speq1;

	fnorm=(float)nn1*(float)nn2*(float)nn3/2.0;
	data1=f3tensor(1,nn1,1,nn2,1,nn3);
	data2=f3tensor(1,nn1,1,nn2,1,nn3);
	speq1=matrix(1,nn1,1,nn2<<1);
	for (i=1;i<=nn1;i++)
		for (j=1;j<=nn2;j++)
			for (k=1;k<=nn3;k++)
				data2[i][j][k]=fnorm*(data1[i][j][k]=2*ran1(&idum)-1);
	rlft3(data1,speq1,nn1,nn2,nn3,1);
	/* here would be any processing in Fourier space */
	rlft3(data1,speq1,nn1,nn2,nn3,-1);
	err=compare("data",data1,data2,nn1,nn2,nn3,EPS);
	if (err) {
		printf("Comparison error at tolerance %12.6f\n",EPS);
		printf("Total number of errors is %d\n",err);
	}
	else {
		printf("Data compares OK to tolerance %12.6f\n",EPS);
	}
	free_matrix(speq1,1,nn1,1,nn2<<1);
	free_f3tensor(data2,1,nn1,1,nn2,1,nn3);
	free_f3tensor(data1,1,nn1,1,nn2,1,nn3);
	return (err > 0);
}
Esempio n. 2
0
int main(int argc, char **argv){
	int i,j,k;
	int n = 257; //set problem size here (33, 65, 129, 257)
	
	int ncycle = 2;  
	double dx = 1/(n-1);
	double C = alpha * dt/pow(dx,2);
	double time_diff;
	

	mode = 1; //set as 1 for Jacobi, 2 for GS, 3 for GS with Black-Red ordering
	
	if (mode == 1)
		printf("Starting Jacobi:\n");
	else if (mode == 2)
		printf("Starting Gauss-Siedel:\n");
	else if (mode == 3)
		printf("Starting Gauss-Siedel w/ Red-Black:\n");
	
	time_t start, end;
	FILE *outfile;
	
	double *vec;
	float ***f;

	vec = dvector(1, n);
	f = f3tensor(1, n, 1, n, 1, n);
	
	for (i=1; i<=n; i++)
		vec[i] = i/n;

	//establish initial conditions for matrix and random noise
	for (i=1; i<=n; i++) 
	   for (j=1; j<=n; j++) 
	      for (k=1; k<=n; k++) 
		  {
			f[i][j][k] = exp(
				exp(-pow(5*vec[i]-2.5,2))*
				exp(-pow(5*vec[i]-2.5,2))*
				exp(-pow(5*vec[i]-2.5,2))) 
				* (noise+(rand()%10)/1000);
	      }
		  
	start = clock();
	mglin(f,n,ncycle);
	end = clock();
	
	time_diff = (double)(end-start)/CLOCKS_PER_SEC;
	printf("%lf seconds\n", time_diff);

	outfile = fopen("soln.dat", "w");
	fwrite(&f[1][1][1],sizeof(double),n*n*n,outfile);
	fclose(outfile);
	free_f3tensor(f,1,n,1,n,1,n);
}
Esempio n. 3
0
int main(void)		/* example3 */
{
	void rlft3(float ***data, float **speq, unsigned long nn1,
		unsigned long nn2, unsigned long nn3, int isign);
	int j;
	float fac,r,i,***data1,***data2,**speq1,**speq2,*sp1,*sp2;

	data1=f3tensor(1,N,1,N,1,N);
	data2=f3tensor(1,N,1,N,1,N);
	speq1=matrix(1,N,1,2*N);
	speq2=matrix(1,N,1,2*N);

	rlft3(data1,speq1,N,N,N,1);
	rlft3(data2,speq2,N,N,N,1);
	fac=2.0/(N*N*N);
	sp1 = &data1[1][1][1];
	sp2 = &data2[1][1][1];
	for (j=1;j<=N*N*N/2;j++) {
		r = sp1[0]*sp2[0] - sp1[1]*sp2[1];
		i = sp1[0]*sp2[1] + sp1[1]*sp2[0];
		sp1[0] = fac*r;
		sp1[1] = fac*i;
		sp1 += 2;
		sp2 += 2;
	}
	sp1 = &speq1[1][1];
	sp2 = &speq2[1][1];
	for (j=1;j<=N*N;j++) {
		r = sp1[0]*sp2[0] - sp1[1]*sp2[1];
		i = sp1[0]*sp2[1] + sp1[1]*sp2[0];
		sp1[0] = fac*r;
		sp1[1] = fac*i;
		sp1 += 2;
		sp2 += 2;
	}
	rlft3(data1,speq1,N,N,N,-1);
	free_matrix(speq2,1,N,1,2*N);
	free_matrix(speq1,1,N,1,2*N);
	free_f3tensor(data2,1,N,1,N,1,N);
	free_f3tensor(data1,1,N,1,N,1,N);
	return 0;
}
Esempio n. 4
0
int main(void)		/* example1 */
{
	void rlft3(float ***data, float **speq, unsigned long nn1,
		unsigned long nn2, unsigned long nn3, int isign);
	float ***data, **speq;

	data=f3tensor(1,1,1,N2,1,N3);
	speq=matrix(1,1,1,2*N2);
	rlft3(data,speq,1,N2,N3,1);
	rlft3(data,speq,1,N2,N3,-1);
	free_matrix(speq,1,1,1,2*N2);
	free_f3tensor(data,1,1,1,N2,1,N3);
	return 0;
}
Esempio n. 5
0
void mglin(float ***u, int n, int ncycle){
/*
  Full Multigrid Algorithm for solution of the steady state heat
  equation with forcing.  On input u[1..n][1..n] contains the
  right-hand side ρ, while on output it returns the solution.  The
  dimension n must be of the form 2j + 1 for some integer j. (j is
  actually the number of grid levels used in the solution, called ng
  below.) ncycle is the number of V-cycles to be used at each level.
*/
  unsigned int j,jcycle,jj,jpost,jpre,nf,ng=0,ngrid,nn;
  /*** setup multigrid jagged arrays ***/
  float ***iu[NGMAX+1];   /* stores solution at each grid level */
  float ***irhs[NGMAX+1]; /* stores rhs at each grid level */
  float ***ires[NGMAX+1]; /* stores residual at each grid level */
  float ***irho[NGMAX+1]; /* stores rhs during intial solution of FMG */
  
  /*** use bitshift to find the number of grid levels, stored in ng ***/
  nn=n;                   
  while (nn >>= 1) ng++;     
  
  /*** some simple input checks ***/
  if (n != 1+(1L << ng)) nrerror("n-1 must be a power of 2 in mglin.");
  if (ng > NGMAX) nrerror("increase NGMAX in mglin.");
  
  /***restrict solution to next coarsest grid (irho[ng-1])***/
  nn=n/2+1;
  ngrid=ng-1;
  irho[ngrid]=f3tensor(1,nn,1,nn,1,nn); 
  rstrct(irho[ngrid],u,nn);/* coarsens rhs (u at this point) to irho on mesh size nn */
  
  /***continue setting up coarser grids down to coarsest level***/
  while (nn > 3) { 
    nn=nn/2+1; 
    irho[--ngrid]=f3tensor(1,nn,1,nn,1,nn);
    rstrct(irho[ngrid],irho[ngrid+1],nn); 
  }
  
  /***now setup and solve coarsest level iu[1],irhs[1] ***/
  nn=3;
  iu[1]=f3tensor(1,nn,1,nn,1,nn);
  irhs[1]=f3tensor(1,nn,1,nn,1,nn);
  slvsml(iu[1],irho[1]);          /* solve the small system directly */
  free_f3tensor(irho[1],1,nn,1,nn,1,nn);
  ngrid=ng;                       /* reset ngrid to original size */

  for (j=2;j<=ngrid;j++) {        /* loop over coarse to fine, starting at level 2 */
    printf("at grid level %d\n",j);
    nn=2*nn-1;                     
    iu[j]=f3tensor(1,nn,1,nn,1,nn);     /* setup grids for lhs,rhs, and residual */
    irhs[j]=f3tensor(1,nn,1,nn,1,nn);
    ires[j]=f3tensor(1,nn,1,nn,1,nn);
    interp(iu[j],iu[j-1],nn);
    /* irho contains rhs except on fine grid where it is in u */
    copy(irhs[j],(j != ngrid ? irho[j] : u),nn); 
    /* v-cycle at current grid level */
    for (jcycle=1;jcycle<=ncycle;jcycle++) {  
      /* nf is # points on finest grid for current v-sweep */
      nf=nn;                                  
      for (jj=j;jj>=2;jj--) {                 
	for (jpre=1;jpre<=NPRE;jpre++)  /* NPRE g-s sweeps on the finest (relatively) scale */
	  relax(iu[jj],iu[jj-1],irhs[jj],nf); //need iu[jj-1] for jacobi
	resid(ires[jj],iu[jj],irhs[jj],nf); /* compute res on finest scale, store in ires */
	nf=nf/2+1;                        /* next coarsest scale */
	rstrct(irhs[jj-1],ires[jj],nf);  /* restrict residuals as rhs of next coarsest scale */
	fill0(iu[jj-1],nf);              /* set the initial solution guess to zero */
      } 
      slvsml(iu[1],irhs[1]);                  /* solve the small problem exactly */
      nf=3;                                   /* fine scale now n=3 */
      for (jj=2;jj<=j;jj++) {                 /* work way back up to current finest grid */
	nf=2*nf-1;                            /* next finest scale */
	addint(iu[jj],iu[jj-1],ires[jj],nf);  /* inter error and add to previous solution guess */
	for (jpost=1;jpost<=NPOST;jpost++)    /* do NPOST g-s sweeps */
	  relax(iu[jj],iu[jj-1],irhs[jj],nf);
      }
    }
  }

  copy(u,iu[ngrid],n);              /* copy solution into input array (implicitly returned) */

  /*** clean up memory ***/
  for (nn=n,j=ng;j>=2;j--,nn=nn/2+1) {       
    free_f3tensor(ires[j],1,nn,1,nn,1,nn);      
    free_f3tensor(irhs[j],1,nn,1,nn,1,nn);
    free_f3tensor(iu[j],1,nn,1,nn,1,nn);
    if (j != ng) free_f3tensor(irho[j],1,nn,1,nn,1,nn);
  }
  free_f3tensor(irhs[1],1,3,1,3,1,3);
  free_f3tensor(iu[1],1,3,1,3,1,3);
}
Esempio n. 6
0
void spacodi(int *np, int *ns, double *sp_plot, double *distmat, int *abundtype,
             int *Ndclass, double *dclass, double *Ist_out, double *Pst_out, double *Bst_out, double *PIst_out,
             double *pairwiseIst_out, double *pairwisePst_out, double *pairwiseBst_out, double *pairwisePIst_out) // Modified by jme 01-12-10
{
    int NP,NS,abundt,Ndivc;
    int s1,s2,p1,p2,c, **sps,counter; //Modified by jme 01-12-10
    double **fps, **dist, divc[102];
    double phylod,maxdist,F1,F2;
    float ***DivIijc,***DivPijc,***DivPIijc,***DivSijc;
    double **Ist,**Pst,**Bst,**PIst;
    double divIb[102],divIw[102],divPb[102],divPw[102],divBb[102],divBw[102],divPIb[102],divPIw[102],sumIb,sumIw1,sumIw2,sumPb,sumPw1,sumPw2,sumSb,sumSw1,sumSw2,sumPIb,sumPIw1,sumPIw2;
    double Istc[102],Pstc[102],Bstc[102],PIstc[102];

    NP=(*np);				//# plots
    NS=(*ns);				//# species
    abundt=(*abundtype);	//type of abundance (0, 1 or 2)
    Ndivc=(*Ndclass);		//# of classes of divergence intervals

    fps=dmatrix(0,NP,0,NS);	//frequency per plot and species
    dist=dmatrix(0,NS,0,NS);//divergence matrix between species

    for(s1=1; s1<=NS; s1++)for(s2=1; s2<=NS; s2++) dist[s1][s2]=distmat[(s1-1)+NS*(s2-1)];
    for(p1=1; p1<=NP; p1++)for(s1=1; s1<=NS; s1++) fps[p1][s1]=sp_plot[(s1-1)+NS*(p1-1)];
    for(c=1; c<=Ndivc; c++) divc[c]=dclass[c-1];

    //if dist classes are given, check if last class is larger or equal to max dist,
    // otherwise add a class
    maxdist=0.;
    for(s1=1; s1<NS; s1++)for(s2=s1+1; s2<=NS; s2++) if(maxdist<dist[s1][s2]) maxdist=dist[s1][s2];
    if(Ndivc) {
        if(divc[Ndivc]<maxdist) {
            Ndivc++;
            divc[Ndivc]=maxdist;
            //(*Ndclass)++;
            //dclass[Ndivc-1]=maxdist;
        }
    }
    else {
        Ndivc=1;
        divc[1]=maxdist;
    }

    //identify species present in each plot and attribute a new numerotation (to speed loops)
    // sps[plot][0]=number of species in plot
    // sps[plot][new sp number]=absolute sp number
    // where 'new sp number' ranges from 1 to number of species in plot,
    // and 'absolute sp number' ranges from 1 to NS
    sps=imatrix(0,NP,0,NS);
    for(p1=0; p1<=NP; p1++) {
        sps[p1][0]=0;
        for(s1=1; s1<=NS; s1++) if(fps[p1][s1]) {
                sps[p1][0]++;
                sps[p1][sps[p1][0]]=s1;
            }
    }

    //transform abundances in relative frequencies per plot
    //sum of abundances per plot stored in fps[plot][0]
    for(p1=1; p1<=NP; p1++) {
        fps[p1][0]=0.;
        for(s1=1; s1<=sps[p1][0]; s1++) fps[p1][0]+=fps[p1][sps[p1][s1]];
        for(s1=1; s1<=sps[p1][0]; s1++) fps[p1][sps[p1][s1]]/=fps[p1][0];
    }


    //create 3-dim arrays to store values per pair of plots and per divergence classes
    // c=0 for pairs intra-sp, c=-1 for sums over all classes
    DivIijc=f3tensor(0,NP,0,NP,-1,Ndivc);	//freq of pairs of ind
    DivPijc=f3tensor(0,NP,0,NP,-1,Ndivc);	//mean dist between ind
    DivSijc=f3tensor(0,NP,0,NP,-1,Ndivc);	//freq of pairs of species
    DivPIijc=f3tensor(0,NP,0,NP,-1,Ndivc);	//mean dist between species


    //compute diversity within and between plots
    for(p1=1; p1<=NP; p1++)for(p2=p1; p2<=NP; p2++) {
            for(c=-1; c<=Ndivc; c++) DivIijc[p1][p2][c]=DivPijc[p1][p2][c]=DivSijc[p1][p2][c]=DivPIijc[p1][p2][c]=0.f;

            for(s1=1; s1<=sps[p1][0]; s1++) {
                F1=fps[p1][sps[p1][s1]];
                for(s2=1; s2<=sps[p2][0]; s2++) {
                    F2=fps[p2][sps[p2][s2]];
                    if(p1==p2 && s1==s2 && abundt==2) F2=((F2*fps[p1][0])-1.)/(fps[p1][0]-1.); //sample size correction when abundnaces are individuals counts

                    if(sps[p1][s1]==sps[p2][s2]) {
                        c=0;
                        phylod=0.;
                    }
                    else {
                        phylod=dist[sps[p1][s1]][sps[p2][s2]];	//phyletic distance between species (0 for a species with itself)
                        c=1;
                        while(divc[c]<phylod) c++;
                    }

                    DivIijc[p1][p2][c]+=(float)(F1*F2);				//prob identity
                    DivPijc[p1][p2][c]+=(float)(F1*F2*phylod);		//mean dist btw ind
                    DivSijc[p1][p2][c]++;							//# pairs of sp
                    DivPIijc[p1][p2][c]+=(float)(phylod);			//mean dist btw sp
                }  //end of loop s2
            }  //end loop s1

            //convert into mean dist btw ind or sp per class
            for(c=0; c<=Ndivc; c++) DivPijc[p1][p2][c]/=DivIijc[p1][p2][c];
            for(c=1; c<=Ndivc; c++) DivPIijc[p1][p2][c]/=DivSijc[p1][p2][c];

            //sums
            for(c=0; c<=Ndivc; c++) DivIijc[p1][p2][-1]+=DivIijc[p1][p2][c];
            for(c=1; c<=Ndivc; c++) DivSijc[p1][p2][-1]+=DivSijc[p1][p2][c];

            //proportions
            for(c=0; c<=Ndivc; c++) DivIijc[p1][p2][c]/=DivIijc[p1][p2][-1];
            for(c=1; c<=Ndivc; c++) DivSijc[p1][p2][c]/=DivSijc[p1][p2][-1];

            //mean dist btw ind or sp cumulated over classes
            for(c=1; c<=Ndivc; c++) {
                DivPijc[p1][p2][-1]+=DivPijc[p1][p2][c]*DivIijc[p1][p2][c];
                DivPIijc[p1][p2][-1]+=DivPIijc[p1][p2][c]*DivSijc[p1][p2][c];
            }

        }

    Ist=dmatrix(0,NP,0,NP);
    Pst=dmatrix(0,NP,0,NP);
    Bst=dmatrix(0,NP,0,NP);
    PIst=dmatrix(0,NP,0,NP);

    for(c=0; c<=Ndivc; c++) divIb[c]=divIw[c]=divPb[c]=divPw[c]=divBb[c]=divBw[c]=divPIb[c]=divPIw[c]=0.;
    //pairwise Ist & cie
    counter=0; //Modified by jme 01-12-10
    for(p1=1; p1<=NP; p1++)for(p2=p1+1; p2<=NP; p2++) {
            pairwiseIst_out[counter]=Ist[p1][p2]=Ist[p2][p1]=1.- (((1.-DivIijc[p1][p1][0])+(1.-DivIijc[p2][p2][0]))/2.) / (1.-DivIijc[p1][p2][0]); //Modified by jme 01-12-10
            pairwisePst_out[counter]=Pst[p1][p2]=Pst[p2][p1]=1.- ((DivPijc[p1][p1][-1]+DivPijc[p2][p2][-1])/2.) / DivPijc[p1][p2][-1]; //Modified by jme 01-12-10
            pairwiseBst_out[counter]=Bst[p1][p2]=Bst[p2][p1]=1.- ((DivPijc[p1][p1][-1]/(1.-DivIijc[p1][p1][0])+DivPijc[p2][p2][-1]/(1.-DivIijc[p2][p2][0]))/2.) / (DivPijc[p1][p2][-1]/(1.-DivIijc[p1][p2][0])); //Modified by jme 01-12-10
            pairwisePIst_out[counter]=PIst[p1][p2]=PIst[p2][p1]=1.- ((DivPIijc[p1][p1][-1]+DivPIijc[p2][p2][-1])/2.) / DivPIijc[p1][p2][-1]; //Modified by jme 01-12-10
            counter=counter+1; //Modified by jme 01-12-10

            //sums for global stat
            divIb[0]+=(1.-DivIijc[p1][p2][0]);
            divIw[0]+=( (1.-DivIijc[p1][p1][0]) + (1.-DivIijc[p2][p2][0]) )/2.;
            divPb[0]+=DivPijc[p1][p2][-1];
            divPw[0]+=(DivPijc[p1][p1][-1]+DivPijc[p2][p2][-1])/2.;
            divBb[0]+=DivPijc[p1][p2][-1]/(1.-DivIijc[p1][p2][0]);
            divBw[0]+=( DivPijc[p1][p1][-1]/(1.-DivIijc[p1][p1][0]) + DivPijc[p2][p2][-1]/(1.-DivIijc[p2][p2][0]) )/2.;
            divPIb[0]+=DivPIijc[p1][p2][-1];
            divPIw[0]+=(DivPIijc[p1][p1][-1]+DivPIijc[p2][p2][-1])/2.;
        }
    Istc[0]=1.-divIw[0]/divIb[0];
    Pstc[0]=1.-divPw[0]/divPb[0];
    Bstc[0]=1.-divBw[0]/divBb[0];
    PIstc[0]=1.-divPIw[0]/divPIb[0];

    //global Ist by dist classes
    for(p1=1; p1<=NP; p1++)for(p2=p1+1; p2<=NP; p2++) {
            sumIb=sumIw1=sumIw2=sumPb=sumPw1=sumPw2=sumSb=sumSw1=sumSw2=sumPIb=sumPIw1=sumPIw2=0.;
            for(c=1; c<=Ndivc; c++) {
                sumIb+=DivIijc[p1][p2][c];
                sumIw1+=DivIijc[p1][p1][c];
                sumIw2+=DivIijc[p2][p2][c];
                sumPb+=DivPijc[p1][p2][c]*DivIijc[p1][p2][c];
                sumPw1+=DivPijc[p1][p1][c]*DivIijc[p1][p1][c];
                sumPw2+=DivPijc[p2][p2][c]*DivIijc[p2][p2][c];
                divBb[c]+=sumPb/sumIb;
                divBw[c]+=((sumPw1/sumIw1)+(sumPw2/sumIw2))/2.;
                divPb[c]+=sumPb/(sumIb+DivIijc[p1][p2][0]);
                divPw[c]+=((sumPw1/(sumIw1+DivIijc[p1][p1][0]))+(sumPw2/(sumIw2+DivIijc[p2][p2][0])))/2.;

                sumSb+=DivSijc[p1][p2][c];
                sumSw1+=DivSijc[p1][p1][c];
                sumSw2+=DivSijc[p2][p2][c];
                sumPIb+=DivPIijc[p1][p2][c]*DivSijc[p1][p2][c];
                sumPIw1+=DivPIijc[p1][p1][c]*DivSijc[p1][p1][c];
                sumPIw2+=DivPIijc[p2][p2][c]*DivSijc[p2][p2][c];
                divPIb[c]+=sumPIb/sumSb;
                divPIw[c]+=((sumPIw1/sumSw1)+(sumPIw2/sumSw2))/2.;
            }
        }
    for(c=1; c<=Ndivc; c++) {
        Pstc[c]=1.-divPw[c]/divPb[c];
        Bstc[c]=1.-divBw[c]/divBb[c];
        PIstc[c]=1.-divPIw[c]/divPIb[c];
    }



    (*Ist_out)=Istc[0]; // Modified by cetp 17-08-09
    (*Pst_out)=Pstc[0]; // Modified by cetp 17-08-09
    (*Bst_out)=Bstc[0]; // Modified by cetp 17-08-09
    (*PIst_out)=PIstc[0];// Modified by cetp 17-08-09

    //free memory
    free_dmatrix(fps,0,NP,0,NS);
    free_dmatrix(dist,0,NS,0,NS);
    free_imatrix(sps,0,NP,0,NS);
    free_f3tensor(DivIijc,0,NP,0,NP,-1,Ndivc);
    free_f3tensor(DivPijc,0,NP,0,NP,-1,Ndivc);
    free_f3tensor(DivSijc,0,NP,0,NP,-1,Ndivc);
    free_f3tensor(DivPIijc,0,NP,0,NP,-1,Ndivc);
    free_dmatrix(Ist,0,NP,0,NP);
    free_dmatrix(Pst,0,NP,0,NP);
    free_dmatrix(Bst,0,NP,0,NP);
    free_dmatrix(PIst,0,NP,0,NP);


}
Esempio n. 7
0
//ComTraitMetric - calculate metrics of trait variation in each sample
void ComTraitMetric(sample S, traits T, int SwapMethod, int XVAR)
{

  //initialize variables
  int samp,trait, rec, run;
  int *STattach;
  int **rankLowSTMetricRnd;
  int **rankHighSTMetricRnd;
  float **STMeanObs;
  float ***STMeanRnd;
  float **MeanSTMetricRnd;
  float **STMetricObs;
  float ***STMetricRnd;
  float **StDevSTMetricRnd;
  float **SESSTMetricRnd;
  float *sampleTraits;
  float *randomMetrics;
  float mean, var, metric;

  rankLowSTMetricRnd = imatrix(0,S.nsamples-1,0,T.ntraits-1);
  rankHighSTMetricRnd = imatrix(0,S.nsamples-1,0,T.ntraits-1);

  STMeanObs = matrix(0,S.nsamples-1,0,T.ntraits-1);
  STMeanRnd = f3tensor(0,S.nsamples-1,0,T.ntraits-1,0,RUNS);
  MeanSTMetricRnd = matrix(0,S.nsamples-1,0,T.ntraits-1);

  STMetricObs = matrix(0,S.nsamples-1,0,T.ntraits-1);
  STMetricRnd = f3tensor(0,S.nsamples-1,0,T.ntraits-1,0,RUNS);
  StDevSTMetricRnd = matrix(0,S.nsamples-1,0,T.ntraits-1);

  SESSTMetricRnd = matrix(0,S.nsamples-1,0,T.ntraits-1);

  // attach sample to traits
  STattach = ivector(0, T.ntaxa-1);
  AttachSampleToTraits(S,T,STattach);

  //loop through samples and traits
  //calculate trait metric of each sample
  //store in an array
  for (trait = 0; trait < T.ntraits; trait++)
  {
    for (samp = 0; samp < S.nsamples; samp++)
    {
    	  //(re)dimension a vector to fill with trait values for this sample
    	  sampleTraits = vector(0,S.srec[samp]-1);

    	  //fill the vector
		  for (rec = 0;rec < S.srec[samp];rec++)
		  {
		  	sampleTraits[rec] = T.tr[STattach[ S.id[samp][rec] ]][trait];
		  }

		  //calculate the metric of the trait vector for this sample/trait combo
		  meanvar(sampleTraits, S.srec[samp], &mean, &metric);
		  STMeanObs[samp][trait] = mean;
		  traitMetric(sampleTraits, S.srec[samp], &metric, XVAR);
			STMetricObs[samp][trait] = metric;
		  free_vector(sampleTraits,0,S.srec[samp]-1);
    }
  }

	// if BURNIN > 0 and independent or trial swap null, randomize and discard (burn in)
	if ((BURNIN > 0) && (SwapMethod==3 || SwapMethod==4)) {
		//burn in using appropriate algorithm
		if (SwapMethod == 3) {
			IndependentSwap(S, BURNIN);
		} else {
			TrialSwap(S, BURNIN);
		}
	}

  //now repeat calculations for randomized matrices
  for (run = 0;run<RUNS;run++)
  {

    	//Swap the sample or trait labels
		switch (SwapMethod)
			{
			case 0:
			  //SwapMethod 0 = shuffle traits across species
			  //(sample remains unshuffled)
			  TraitsAttachShuffle(S,T,STattach);
			  break;
			case 1:
			  //SwapMethod 1 = samples become random draws from sample taxa list
			  //(maintains sample species richnesses but not species frequencies)
			  RandomizeSampleTaxaShuffle(S);
			  break;
			case 2:
			  //SwapMethod 2 = samples become random draws from traits
				//note this is redundant for traits but included for completeness
			  RandomizeSampleTaxaShuffle(S);
			  TraitsAttachShuffle(S,T,STattach);
			  break;
			case 3:
			  //SwapMethod 3 = independent checkerboard swap of sample matrix
			  //(maintains species frequencies and sample species richnesses)
			  IndependentSwap(S,SWAPS);
			  break;
			case 4:
				//SwapMethod 4 = trial swap of sample matrix
				//(maintains species frequencies and sample species richnesses)
				TrialSwap(S, SWAPS);
				break;
			default:
			  printf("Please use -m command line switch to specify a randomization method.\n");
			  printf("See documentation for a list of possible null models.\n");
			  exit(EXIT_FAILURE);
			  break;
			}

	  for (trait = 0; trait < T.ntraits; trait++)
	  {
	    for (samp = 0; samp < S.nsamples; samp++)
	    {
	    	  //(re)dimension a vector to fill with trait values for this sample
	    	  sampleTraits = vector(0,S.srec[samp]-1);

	    	  //fill the vector
		  for (rec = 0;rec < S.srec[samp];rec++)
		  {
		  	sampleTraits[rec] = T.tr[STattach[ S.id[samp][rec] ]][trait];
		  }

	    //calculate the metric of the trait vector for this sample/trait combo
		  meanvar(sampleTraits, S.srec[samp], &mean, &metric);
		  STMeanRnd[samp][trait][run] = mean;
		  traitMetric(sampleTraits, S.srec[samp], &metric, XVAR);
		  STMetricRnd[samp][trait][run] = metric;
		  free_vector(sampleTraits,0,S.srec[samp]-1);
	    }
	  }
  }

  //calculate summary statistics for randomized data
  for (trait = 0;trait < T.ntraits;trait++)
  {
    for (samp = 0;samp < S.nsamples;samp++)
    {
    	  randomMetrics = vector(0,RUNS-1);
    	  for (run = 0;run < RUNS;run++)
    	  {
    	  	randomMetrics[run] = STMetricRnd[samp][trait][run];
    	  	if (randomMetrics[run] > STMetricObs[samp][trait]) rankHighSTMetricRnd[samp][trait]++;
    	  	if (randomMetrics[run] < STMetricObs[samp][trait]) rankLowSTMetricRnd[samp][trait]++;
    	  }
      meanvar(randomMetrics,RUNS,&mean,&var);
      MeanSTMetricRnd[samp][trait] = mean;
      StDevSTMetricRnd[samp][trait] = sqrt(var);
      SESSTMetricRnd[samp][trait] = (STMetricObs[samp][trait] - MeanSTMetricRnd[samp][trait]) \
      									/ StDevSTMetricRnd[samp][trait];
      free_vector(randomMetrics,0,RUNS-1);
    }
  }

  //summarize and output

  //print header
	printf("Phylocom output: randomization method %d, %d runs, trait metric %d (",SwapMethod,RUNS,XVAR);
	switch (XVAR)
	{
		case 1:
		  	printf("variance)\n");
		  break;
		case 2:
		  	printf("MPD)\n");
		  break;
		case 3:
		  	printf("MNTD)\n");
		  break;
		case 4:
		  	printf("range)\n");
		  break;
		default:
			printf(")\n");
		  break;
}

  //print results with separate entry for each trait/plot
  printf("Trait\tSample\tNTaxa\tMean\tMetric\tMeanRndMetric\tSDRndMetric\tSESMetric\trankLow\trankHigh\truns\n");
  for (trait = 0;trait < T.ntraits;trait++)
  {
    for (samp = 0;samp < S.nsamples;samp++)
    {
      printf("%s\t%s\t%d\t%f\t%f\t%f\t%f\t%f\t%d\t%d\t%d\n",T.trname[trait],S.pname[samp],S.srec[samp],STMeanObs[samp][trait],STMetricObs[samp][trait],MeanSTMetricRnd[samp][trait],StDevSTMetricRnd[samp][trait],SESSTMetricRnd[samp][trait],rankLowSTMetricRnd[samp][trait],rankHighSTMetricRnd[samp][trait],RUNS);
    }
  }

}
Esempio n. 8
0
int main(void)  /* Program sfroid */
{
	float plgndr(int l, int m, float x);
	void solvde(int itmax, float conv, float slowc, float scalv[],
		int indexv[], int ne, int nb, int m, float **y, float ***c, float **s);
	int i,itmax,k,indexv[NE+1];
	float conv,deriv,fac1,fac2,q1,slowc,scalv[NE+1];
	float **y,**s,***c;

	y=matrix(1,NYJ,1,NYK);
	s=matrix(1,NSI,1,NSJ);
	c=f3tensor(1,NCI,1,NCJ,1,NCK);
	itmax=100;
	conv=5.0e-6;
	slowc=1.0;
	h=1.0/(M-1);
	printf("\nenter m n\n");
	scanf("%d %d",&mm,&n);
	if (n+mm & 1) {
		indexv[1]=1;
		indexv[2]=2;
		indexv[3]=3;
	} else {
		indexv[1]=2;
		indexv[2]=1;
		indexv[3]=3;
	}
	anorm=1.0;
	if (mm) {
		q1=n;
		for (i=1;i<=mm;i++) anorm = -0.5*anorm*(n+i)*(q1--/i);
	}
	for (k=1;k<=(M-1);k++) {
		x[k]=(k-1)*h;
		fac1=1.0-x[k]*x[k];
		fac2=exp((-mm/2.0)*log(fac1));
		y[1][k]=plgndr(n,mm,x[k])*fac2;
		deriv = -((n-mm+1)*plgndr(n+1,mm,x[k])-
			(n+1)*x[k]*plgndr(n,mm,x[k]))/fac1;
		y[2][k]=mm*x[k]*y[1][k]/fac1+deriv*fac2;
		y[3][k]=n*(n+1)-mm*(mm+1);
	}
	x[M]=1.0;
	y[1][M]=anorm;
	y[3][M]=n*(n+1)-mm*(mm+1);
	y[2][M]=(y[3][M]-c2)*y[1][M]/(2.0*(mm+1.0));
	scalv[1]=fabs(anorm);
	scalv[2]=(y[2][M] > scalv[1] ? y[2][M] : scalv[1]);
	scalv[3]=(y[3][M] > 1.0 ? y[3][M] : 1.0);
	for (;;) {
		printf("\nEnter c**2 or 999 to end.\n");
		scanf("%f",&c2);
		if (c2 == 999) {
			free_f3tensor(c,1,NCI,1,NCJ,1,NCK);
			free_matrix(s,1,NSI,1,NSJ);
			free_matrix(y,1,NYJ,1,NYK);
			return 0;
		}
		solvde(itmax,conv,slowc,scalv,indexv,NE,NB,M,y,c,s);
		printf("\n %s %2d %s %2d %s %7.3f %s %10.6f\n",
			"m =",mm,"  n =",n,"  c**2 =",c2,
			" lamda =",y[3][1]+mm*(mm+1));
	}
}
int main(int argc,char *argv[])
{
	char *listfile,*inputfile;
	char choose= ' ';
	string operate=" ";
	double temperature=300;
	string output=" ";
	string segment="all";
	int segment_b=0;
	int segment_e=0;
//  读取当前的时间
	time_t t = time( 0 ); 
    char TIME[64]; 
    strftime( TIME, sizeof(TIME), "%Y/%m/%d %X ",localtime(&t) ); 
    
//

	switch(argc)
	{
	case 1:
		cout<<"Usage:CurAnalysis -f listfile.txt -i inputfile.txt"<<endl;
		
		exit(0);
	case 2:
		if(string(argv[1])=="-a")
		{
			Printversion();
			exit(0);
		}
		if(string(argv[1])=="-h")
		{
			Printhelp();
			exit(0);
		}
	case 5:
		if(string(argv[1])=="-f"&&string(argv[3])=="-i")
		{
			listfile=argv[2];
			inputfile=argv[4];
		}
		break;

	default:
		cout<<"Usage:CurAnalysis -f listfile.txt -i inputfile.txt"<<endl;
		exit(0);

	}

	ReadInput(inputfile,operate,choose,output,temperature,segment,segment_b,segment_e);
/*
average部分已经完成,经测试可以使用,主要的问题是读文件的异常
比如说文件残缺,文件丢失等的情况的处理的过程没有完成。
*/
	if(operate=="average")
	{
		if(choose=='E'||choose=='F'||choose=='G'||choose=='H')
		{
			double ***totaldata;
			struct	Filelist *fl;
			struct Paramater *spt;
			int N=0;
			ofstream outfile(output.c_str(),ios::app);
			fl=Readlist(listfile);
			switch(choose)
			{
			case 'E':
				spt=E_part(fl->file);
				break;
			case 'F':
				spt=F_part(fl->file);
				break;
			case 'G':
				spt=G_part(fl->file);
				break;
			case 'H':
				spt=H_part(fl->file);
				break;
			}
			int filesize=Getfilelen(fl);	
			int size=GetParamaterlen(spt);
			if(segment!="all")
			{
				size=segment_e - segment_b+1;
			}
			if(segment=="all")
			{
				segment_b=1;
				segment_e=size;
			}
			totaldata=f3tensor(0,5,0,size-1,0,filesize-1);
outfile<<"******************************************************************"<<endl;
//output the input file!
	ifstream infile(inputfile);
	string s;
    getline(infile,s);
   while(!infile.fail())
	{
		outfile<<"#"<<s<<endl;
		getline(infile,s);
	}
    outfile<<TIME<<endl;  //输出当前时间
	infile.close();

//
			while(fl!=NULL)
			{
				struct Paramater *sp; 
				switch(choose)
				{
					case 'E':
						sp=E_part(fl->file);
					break;
					case 'F':
						sp=F_part(fl->file);
					break;
					case 'G':
						sp=G_part(fl->file);
					break;
					case 'H':
						sp=H_part(fl->file);
					break;
				}
		//		int size=GetParamaterlen(sp);
				double **data;
				data=dmatrix(0,5,0,size-1);
				struct Namelist *name;
				name=(struct Namelist *)malloc(sizeof(struct Namelist));
				Paramater2double(sp,data,name,segment_b,segment_e);

				for(int i=0;i<6;i++)
				{
					for(int j=0;j<size;j++)
					{
						totaldata[i][j][N]=data[i][j];
					}
				}
				free_dmatrix(data,0,5,0,size-1);
				cout<<"Read file "<<fl->file<<" finished!"<<endl;//输出提示
				fl=fl->next;
				N++;
			}

			double **aveg;
			aveg=dmatrix(0,5,0,size-1);
			for(int i=0;i<6;i++)
			{
				for(int j=0;j<size;j++)
				{
					aveg[i][j]=0;
				}
			}

			outfile<<endl;
			outfile<<"******************************************************************"<<endl;
			outfile<<"\t"<<"rise"<<"\t"<<"roll"<<"\t"<<"shift"<<"\t"<<"slide"<<"\t"<<"tilt"<<"\t"<<"twist"<<endl;
			outfile<<endl;

			for(int i=0;i<size;i++)
			{
				outfile<<i+segment_b<<"\t";
				for(int j=0;j<6;j++)
				{
					for(int k=0;k<filesize;k++)
					{
						aveg[j][i]+=totaldata[j][i][k];
					}
					aveg[j][i]=aveg[j][i]/filesize;
					outfile<<fixed<<showpoint;
					
	 				outfile<<setprecision(2)<<aveg[j][i]<<"\t";
				}
				outfile<<endl;
			}
			outfile<<endl;
			outfile<<"******************************************************************"<<endl;
		   cout<<"finished the calculation of average !"<<endl;
		   cout<<"the output write in the file "<<output<<" !"<<endl;
			outfile.close();
			free_f3tensor(totaldata,0,5,0,size-1,0,filesize-1);
		}

		//average for J

		if(choose=='J')
		{
			double ***totaldata;
			struct	Filelist *fl;
			struct Backbone *spt;
			int N=0;
			fl=Readlist(listfile);
			spt=J_part(fl->file);
			int filesize=Getfilelen(fl);	
			int size=GetBackbonelen(spt);
			if(segment!="all")
			{
				size=segment_e - segment_b+1;
			}
			if(segment=="all")
			{
				segment_b=1;
				segment_e=size;
			}
			totaldata=f3tensor(0,13,0,size-1,0,filesize-1);
			ofstream outfile(output.c_str(),ios::app);

			outfile<<"******************************************************************"<<endl;
		//output the input file!
			ifstream infile(inputfile);
			string s;
			getline(infile,s);
			 while(!infile.fail())
			{
				outfile<<"#"<<s<<endl;
				getline(infile,s);
			}
			 outfile<<TIME<<endl; //输出当前时间
			infile.close();
//

			while(fl!=NULL)
			{
				struct Backbone *sp;
				sp=J_part(fl->file);
		//		int size=GetBackbonelen(sp);
				double **data;
				data=dmatrix(0,13,0,size-1);
				struct Namelist *name;
				name=(struct Namelist *)malloc(sizeof(struct Namelist));
				Backbone2double(sp,data,name,segment_b,segment_e);

				for(int i=0;i<14;i++)
				{
					for(int j=0;j<size;j++)
					{
						totaldata[i][j][N]=data[i][j];
					}
				}
				free_dmatrix(data,0,13,0,size-1);
				cout<<"Read file "<<fl->file<<" finished!"<<endl;//输出提示
				fl=fl->next;
				N++;
			}

			double **aveg;
			aveg=dmatrix(0,13,0,size-1);
			for(int i=0;i<14;i++)
			{
				for(int j=0;j<size;j++)
				{
					aveg[i][j]=0;
				}
			}

			outfile<<endl;
			outfile<<"******************************************************************"<<endl;
			outfile<<"\t"<<"alpha"<<"\t"<<"ampli"<<"\t"<<"beta"<<"\t"<<"c1"<<"\t"<<"c1c2"<<"\t"<<"c2"<<"\t"<<"c2c3"<<"\t"<<"c3"<<"\t"<<"chi"<<"\t"<<"delta"<<"\t"<<"epsil"<<"\t"<<"gamma"<<"\t"<<"phase"<<"\t"<<"zeta"<<endl;
			outfile<<endl;

			for(int i=0;i<size;i++)
			{
				outfile<<i+segment_b<<"\t";
				for(int j=0;j<14;j++)
				{
					for(int k=0;k<filesize;k++)
					{
						aveg[j][i]+=totaldata[j][i][k];
					}
					aveg[j][i]=aveg[j][i]/filesize;
					outfile<<fixed<<showpoint;
					outfile<<setprecision(2)<<aveg[j][i]<<"\t";
				}
				outfile<<endl;
			}
			outfile<<endl;
			outfile<<"******************************************************************"<<endl;
		   cout<<"finished the calculation of average !"<<endl;
		   cout<<"the output write in the file "<<output<<" !"<<endl;
			outfile.close();
			free_f3tensor(totaldata,0,13,0,size-1,0,filesize-1);
		}

	}

	
//计算某一数据的分布


	if(operate=="distribution")
	{
		//part 1: get the data
		if(choose=='E'||choose=='F'||choose=='G'||choose=='H')
		{
			double ***totaldata;
			struct	Filelist *fl;
			struct Paramater *spt;
			int N=0;
			ofstream outfile(output.c_str(),ios::app);
			fl=Readlist(listfile);
			switch(choose)
			{
			case 'E':
				spt=E_part(fl->file);
				break;
			case 'F':
				spt=F_part(fl->file);
				break;
			case 'G':
				spt=G_part(fl->file);
				break;
			case 'H':
				spt=H_part(fl->file);
				break;
			}
			int filesize=Getfilelen(fl);	
			int size=GetParamaterlen(spt);
			if(segment!="all")
			{
				size=segment_e - segment_b+1;
			}
			if(segment=="all")
			{
				segment_b=1;
				segment_e=size;
			}
			totaldata=f3tensor(0,5,0,size-1,0,filesize-1);
			outfile<<"******************************************************************"<<endl;

			//output the input file!
			ifstream infile(inputfile);
			string s;
			 getline(infile,s);
			while(!infile.fail())
			{
				outfile<<"#"<<s<<endl;
				getline(infile,s);
			}
		    outfile<<TIME<<endl;  //输出当前时间
			infile.close();

//
			while(fl!=NULL)
			{
				struct Paramater *sp; 
				switch(choose)
				{
					case 'E':
						sp=E_part(fl->file);
					break;
					case 'F':
						sp=F_part(fl->file);
					break;
					case 'G':
						sp=G_part(fl->file);
					break;
					case 'H':
						sp=H_part(fl->file);
					break;
				}
		//		int size=GetParamaterlen(sp);
				double **data;
				data=dmatrix(0,5,0,size-1);
				struct Namelist *name;
				name=(struct Namelist *)malloc(sizeof(struct Namelist));
				Paramater2double(sp,data,name,segment_b,segment_e);

				for(int i=0;i<6;i++)
				{
					for(int j=0;j<size;j++)
					{
						totaldata[i][j][N]=data[i][j];
					}
				}
				free_dmatrix(data,0,5,0,size-1);
				cout<<"Read file "<<fl->file<<" finished!"<<endl;//输出提示
				fl=fl->next;
				N++;
			}
// finished the input data
			//calculate the distribution!
			double *vect;
			vect=dvector(0, size*filesize-1) ;  
			for(int i=0;i<6;i++)
			{
				outfile<<"******************************************************************"<<endl;
				switch(i)
				{
					case 0:
						outfile<<"the distribution of RISE"<<endl;
						break;
					case 1:
						outfile<<"the distribution of ROLL"<<endl;
						break;
					case 2:
						outfile<<"the distribution of SHIFT"<<endl;
						break;
					case 3:
						outfile<<"the distribution of SLIDE"<<endl;
						break;
					case 4:
						outfile<<"the distribution of TILT"<<endl;
						break;
					case 5:
						outfile<<"the distribution of TWIST"<<endl;
						break;
				}
				outfile<<"******************************************************************"<<endl;
				//
				for(int m=0;m<size;m++)
				{
					for(int n=0;n<filesize;n++)
					{
						vect[m*filesize+n]=totaldata[i][m][n];
					}
				}
				//把二维的数据转化为一维的数据!
				
				Distribution(vect,size*filesize,output);
				outfile<<"******************************************************************"<<endl;
			}
		   cout<<"finished the calculation of distribution !"<<endl;
		   cout<<"the output write in the file "<<output<<" !"<<endl;
			outfile.close();
			free_f3tensor(totaldata,0,5,0,size-1,0,filesize-1);
			// finished the calculation !



		}

		if(choose=='J')
		{
			double ***totaldata;
			struct	Filelist *fl;
			struct Backbone *spt;
			int N=0;
			fl=Readlist(listfile);
			spt=J_part(fl->file);
			int filesize=Getfilelen(fl);	
			int size=GetBackbonelen(spt);
			if(segment!="all")
			{
				size=segment_e - segment_b+1;
			}
			if(segment=="all")
			{
				segment_b=1;
				segment_e=size;
			}
			totaldata=f3tensor(0,13,0,size-1,0,filesize-1);
			ofstream outfile(output.c_str(),ios::app);

			outfile<<"******************************************************************"<<endl;
		//output the input file!
			ifstream infile(inputfile);
			string s;
			getline(infile,s);
			 while(!infile.fail())
			{
				outfile<<"#"<<s<<endl;
				getline(infile,s);
			}
			 outfile<<TIME<<endl; //输出当前时间
			infile.close();
//

			while(fl!=NULL)
			{
				struct Backbone *sp;
				sp=J_part(fl->file);
		//		int size=GetBackbonelen(sp);
				double **data;
				data=dmatrix(0,13,0,size-1);
				struct Namelist *name;
				name=(struct Namelist *)malloc(sizeof(struct Namelist));
				Backbone2double(sp,data,name,segment_b,segment_e);

				for(int i=0;i<14;i++)
				{
					for(int j=0;j<size;j++)
					{
						totaldata[i][j][N]=data[i][j];
					}
				}
				free_dmatrix(data,0,13,0,size-1);
				cout<<"Read file "<<fl->file<<" finished!"<<endl;//输出提示
				fl=fl->next;
				N++;
			}

			double *vect;
			vect=dvector(0, size*filesize-1) ;  
			for(int i=0;i<14;i++)
			{
				outfile<<"******************************************************************"<<endl;
				switch(i)
				{
					case 0:
						outfile<<"the distribution of ALPHA"<<endl;
						break;
					case 1:
						outfile<<"the distribution of AMPLI"<<endl;
						break;
					case 2:
						outfile<<"the distribution of BETA"<<endl;
						break;
					case 3:
						outfile<<"the distribution of C1'"<<endl;
						break;
					case 4:
						outfile<<"the distribution of C1'-C2'"<<endl;
						break;
					case 5:
						outfile<<"the distribution of C2'"<<endl;
						break;
					case 6:
						outfile<<"the distribution of C2'-C3'"<<endl;
						break;
				    case 7:
						outfile<<"the distribution of C3'"<<endl;
						break;
					case 8:
						outfile<<"the distribution of CHI"<<endl;
						break;
					case 9:
						outfile<<"the distribution of DELTA"<<endl;
						break;
					case 10:
						outfile<<"the distribution of ESPIL"<<endl;
						break;
					case 11:
						outfile<<"the distribution of GAMMA"<<endl;
						break;
					case 12:
						outfile<<"the distribution of PHASE"<<endl;
						break;
					case 13:
						outfile<<"the distribution of ZETA"<<endl;
						break;
					}
				outfile<<"******************************************************************"<<endl;
				//
				for(int m=0;m<size;m++)
				{
					for(int n=0;n<filesize;n++)
					{
						vect[m*filesize+n]=totaldata[i][m][n];
					}
				}
				//把二维的数据转化为一维的数据!
				
				Distribution(vect,size*filesize,output);
				outfile<<"******************************************************************"<<endl;
			}
		   cout<<"finished the calculation of distribution !"<<endl;
		   cout<<"the output write in the file "<<output<<" !"<<endl;
			outfile.close();
			free_f3tensor(totaldata,0,13,0,size-1,0,filesize-1);
			// finished the calculation !
		}

	}
/*计算刚性*/
	
	if(operate=="stiffness")
	{
		/*get the data*/
		if(choose=='E'||choose=='F'||choose=='G'||choose=='H')
		{
			double ***totaldata;
			struct	Filelist *fl;
			struct Paramater *spt;
			int N=0;
			ofstream outfile(output.c_str(),ios::app);
			fl=Readlist(listfile);
			switch(choose)
			{
			case 'E':
				spt=E_part(fl->file);
				break;
			case 'F':
				spt=F_part(fl->file);
				break;
			case 'G':
				spt=G_part(fl->file);
				break;
			case 'H':
				spt=H_part(fl->file);
				break;
			}
			int filesize=Getfilelen(fl);	
			int size=GetParamaterlen(spt);
			if(segment!="all")
			{
				size=segment_e - segment_b+1;
			}
			if(segment=="all")
			{
				segment_b=1;
				segment_e=size;
			}
			totaldata=f3tensor(0,5,0,size-1,0,filesize-1);
			outfile<<"******************************************************************"<<endl;

			//output the input file!
			ifstream infile(inputfile);
			string s;
			 getline(infile,s);
			while(!infile.fail())
			{
				outfile<<"#"<<s<<endl;
				getline(infile,s);
			}
		    outfile<<TIME<<endl;  //输出当前时间
			infile.close();

//
			while(fl!=NULL)
			{
				struct Paramater *sp; 
				switch(choose)
				{
					case 'E':
						sp=E_part(fl->file);
					break;
					case 'F':
						sp=F_part(fl->file);
					break;
					case 'G':
						sp=G_part(fl->file);
					break;
					case 'H':
						sp=H_part(fl->file);
					break;
				}
		//		int size=GetParamaterlen(sp);
				double **data;
				data=dmatrix(0,5,0,size-1);
				struct Namelist *name;
				name=(struct Namelist *)malloc(sizeof(struct Namelist));
				Paramater2double(sp,data,name,segment_b,segment_e);

				for(int i=0;i<6;i++)
				{
					for(int j=0;j<size;j++)
					{
						totaldata[i][j][N]=data[i][j];
					}
				}
				free_dmatrix(data,0,5,0,size-1);
				cout<<"Read file "<<fl->file<<" finished!"<<endl;//输出提示
				fl=fl->next;
				N++;
			}
// finished the input data
			/*calculate the stiffness! */
			double **stiff;
			double **matrix;
			double **inve_matrix;

			/*output the title */
				outfile<<"******************************************************************"<<endl;
				outfile<<"calculate the stiffness of part "<<choose<<endl;
				outfile<<"******************************************************************"<<endl;
				
		for(int x=segment_b-1;x<segment_e;x++)   /* the loop for segment  */
		{
			stiff=dmatrix(0,5,0, filesize-1) ;  /* hold the space!*/
			matrix=dmatrix(0,5,0,5);
			inve_matrix=dmatrix(0,5,0,5);

			for(int i=0;i<6;i++)
			{
					for(int n=0;n<filesize;n++)
					{
						stiff[i][n]=totaldata[i][x][n];
					}			
			}

			Getmatrix(stiff,matrix,filesize);  /*cteate the helical matrix!*/
			free_dmatrix(stiff,0,5,0,filesize-1);
			inverse_matrix(matrix, inve_matrix,6);
			free_dmatrix(matrix,0,5,0,5);

			outfile<<"the stiffness of segment "<<x+1<<endl;
			outfile<<"******************************************************************"<<endl;
			outfile<<"\t"<<"rise"<<"\t"<<"roll"<<"\t"<<"shift"<<"\t"<<"slide"<<"\t"<<"tilt"<<"\t"<<"twist"<<endl;
			outfile<<fixed<<showpoint;
			for(int i=0;i<6;i++)
			{
				switch(i)
				{
				case 0:
					outfile<<"rise"<<"\t";
					break;
				case 1:
					outfile<<"roll"<<"\t";
					break;
				case 2:
					outfile<<"shift"<<"\t";
					break;
				case 3:
					outfile<<"slide"<<"\t";
					break;
				case 4:
					outfile<<"tilt"<<"\t";
					break;
				case 5:
					outfile<<"twist"<<"\t";
					break;
				}
				for(int j=0;j<6;j++)
				{
					outfile<<setprecision(2)<<R*temperature*inve_matrix[i][j]<<"\t";
				}
				outfile<<endl;
			}
			outfile<<"******************************************************************"<<endl;
		}
		cout<<"finished the calculation of stiffness !"<<endl;
		cout<<"the output write in the file "<<"\""<<output<<"\"  !"<<endl;
		outfile.close();
		free_f3tensor(totaldata,0,5,0,size-1,0,filesize-1);
			// finished the calculation !

		}
		else
		{
			cout<<"wrong input, please check!"<<endl;
			exit(0);
		}

	}


	if(operate!="stiffness"&&operate!="average"&&operate!="distribution")
	{
		cout<<"Input a wrong operate. the correct oprtate is 'average/distribution/stiffness'"<<endl;
		exit(0);
	}
	return 0;
}