/*
  pA: the pointer of the Matrix A|b
  rowMax,colMax:size of matirx
  pX: solution vector
  return value: -1 solving equation failed
  		 		1 resolve result successfully
*/
int gaussLesung(double *pA, int rowMax,int colMax, double *pX)
{
 	 double *pMatrix = pA;
 	 int i;
 	 double product;
 	 product = 1;
 	 if((1+rowMax)!=colMax) return -1;
 	 
 	 for(i = 0; i < rowMax; i++)
 	 {
  	   int l;
 	   printf("%d,\n",i);
 	   
 	   l = findMax(pMatrix, rowMax, colMax, i,i);
 	   exchangeRow(pMatrix, rowMax, colMax, l, i);
 	   eliminate(pMatrix, rowMax, colMax, i, i);
       outputMatrix(pMatrix, rowMax, colMax);
     }
     
     
     for(i = 0; i < rowMax; i++)product = product * pMatrix[i*colMax+i];
     if(product==0)return -1;
 	 
	  solveX(pMatrix, rowMax,colMax, pX);
	  return 1;
}
Beispiel #2
0
// 从文件中读取矩阵数据
int ReadMatrix(int A[][N], int B[][N])
{
    int i, j;
    FILE *f;
    char fname[] = "matrix.dat";

    if ((f = fopen(fname, "r")) == NULL){
        printf("Open the file '%s' failed\n", fname);
        return 1;
    }
    
    for (i = 0; i < N; i++){
        for (j = 0; j < N; j++){
            if(fscanf(f, "%d", &A[i][j]) == EOF && i * j != (N-1) * (N-1)){
                printf("Read Error!");
                fclose(f);
                return 2;
            }
            B[i][j] = A[i][j];
            //printf("%d\t", A[i][j]);
        }
        //printf("\n");
    }
    fclose(f);
    outputMatrix(A);
    return 0;
}
void HitMatrix::testHitMatrix(void){
	while(true){
		for(int i=0; i<254;++i){
			outputMatrix();
		}
		moveBar();
	}
}
void
SymmAnisotropicElasticityTensor::form_rotated_material_qdmat_matrix()
{
  // The function makes use of Q matrix to rotate
  // Dmat[9][9] to QDmat[9][9]
  // QDmat = QT * Dmat * Q

  DenseMatrix<Real> outputMatrix(9, 9);

  _q.get_transpose(outputMatrix);
  outputMatrix.right_multiply(_dmat);

  _qdmat = outputMatrix;
  _qdmat.right_multiply(_q);
}
Beispiel #5
0
void callMatrix()
{
 int rowMax = 3;
 int colMax = 4;
 int i;
 double *pMatrix=malloc(rowMax*colMax*sizeof(double));
 for(i = 0; i < rowMax*colMax; i++)pMatrix[i]=0; 	 
 pMatrix[0]=1;
 pMatrix[1]=2;
 pMatrix[2]=3;
 pMatrix[3]=14;
 pMatrix[4]=1;
 pMatrix[5]=1;
 pMatrix[6]=1;
 pMatrix[7]=6;
 pMatrix[8]=2;
 pMatrix[9]=1;
 pMatrix[10]=1;
 pMatrix[11]=7;
  double *pX=malloc(rowMax*sizeof(double));
 /*
 for(i = 0; i < rowMax; i++)
 {
  	   int l;
 	   printf("%d,\n",i);
 	   l = findMax(pMatrix, rowMax, colMax, i,i);
 	   exchangeRow(pMatrix, rowMax, colMax, l, i);
 	   eliminate(pMatrix, rowMax, colMax, i, i);
       outputMatrix(pMatrix, rowMax, colMax);
 }
 

 solveX(pMatrix, rowMax,colMax, pX);
 */
   gaussLesung(pMatrix, rowMax, colMax, pX);
   outputMatrix(pX, rowMax, 1);
   free(pX);
   free(pMatrix);
	  
}
Beispiel #6
0
// 进行矩阵乘法计算
int MatrixProduct()
{
    int A[N][N], B[N][N], C[N][N];
    int i, j, k;

    if(ReadMatrix(A, B))
        return 1;

    for (i = 0; i < N; i++){
        for (j = 0; j < N; j++){
            // 注意一定初始化,否则一定出错
            C[i][j] = 0;
            for (k = 0; k < N; k++){
                C[i][j] += A[i][k] * B[k][j];
            }
            //printf("C[%d][%d] = %d\t", i, j, C[i][j]);
        }
        //printf("\n");
    }
    outputMatrix(C);
    return 0;
}
void outputResults(const char* inputFile, double* localRows, double* localSolution, float ddtime, float bstime, float fetime, float pvtime)
{
	// Gather X and U on root node.
	double X[MAX_N];
	double* U = malloc(N * (N + 1) * sizeof(double));
	
	gatherSolution(localSolution, X);
	gatherU(localRows, U);
	
	double cx1, cx2, cu1, cu2;
	
	if(PID == 0)
	{
		computeChecksums(U, X, &cu1, &cu2, &cx1, &cx2);
		
		//float time = mstime() - startTime;
		float sequentialTimeDDBSFEPV = 0;
		float sequentialTimeDD = 0;
		float sequentialTimeBS = 0;
		float sequentialTimeFE = 0;
		float sequentialTimeFENOPV = 0;
		float sequentialTimePV = 0;
		char filename[32];
		
		if(P > 1)
		{
			// Read linear time from saved file (if we are not running in sequential mode)
			sprintf(filename, "op_P_%d_N_%d_R_%d_%d.txt", 1, N, R, DISTRIBUTE_READ);
			FILE* in = fopen(filename, "r");
			
			char line[512];
			// skip first three lines (headers)
			fgets(line, 512, in);
			fgets(line, 512, in);
			fgets(line, 512, in);
			
			fscanf(in, "Sequential Time w/DD+BS     : %f(ms)\n", &sequentialTimeDDBSFEPV);
			fscanf(in, "Sequential Time DD only     : %f(ms)\n", &sequentialTimeDD);
			fscanf(in, "Sequential Time BS only     : %f(ms)\n", &sequentialTimeBS);
			fscanf(in, "Sequential Time PV only     : %f(ms)\n", &sequentialTimePV);
			fscanf(in, "Sequential Time w/oDD+BS    : %f(ms)\n", &sequentialTimeFE);
			fscanf(in, "Sequential Time w/oDD+BS+PV : %f(ms)\n", &sequentialTimeFENOPV);
			fclose(in);
		}
		
		// Write output
		sprintf(filename, "op_P_%d_N_%d_R_%d_%d.txt", P, N, R, DISTRIBUTE_READ);
		FILE* out = fopen(filename, "a");
		
		fprintf(out, "P = %d, distributed_read = %d, N = %d, R = %d, input = %s\n", P, DISTRIBUTE_READ, N, R, inputFile);
		fprintf(out, "Checksum1(X) = %11.4e,    Checksum2(X) = %11.4e,    Checksum1(U) = %11.4e,    Checksum2(U) = %11.4e\n", cx1, cx2, cu1, cu2);
		fprintf(out, "-------------------------------------------------------------------------------------------------------------\n");
		
		if(P == 1)
		{
			// Print times
			fprintf(out, "Sequential Time w/DD+BS     : %.3f(ms)\n", ddtime + fetime + bstime);
			fprintf(out, "Sequential Time DD only     : %.3f(ms)\n", ddtime);
			fprintf(out, "Sequential Time BS only     : %.3f(ms)\n", bstime);
			fprintf(out, "Sequential Time PV only     : %.3f(ms)\n", pvtime);
			fprintf(out, "Sequential Time w/oDD+BS    : %.3f(ms)\n", fetime);
			fprintf(out, "Sequential Time w/oDD+BS+PV : %.3f(ms)\n", fetime - pvtime);
		}
		else
		{
			fprintf(out, "Sequential Time w/DD+BS     : %.3f(ms)\n", sequentialTimeDDBSFEPV);
			fprintf(out, "Sequential Time DD only     : %.3f(ms)\n", sequentialTimeDD);
			fprintf(out, "Sequential Time BS only     : %.3f(ms)\n", sequentialTimeBS);
			fprintf(out, "Sequential Time PV only     : %.3f(ms)\n", sequentialTimePV);
			fprintf(out, "Sequential Time w/oDD+BS    : %.3f(ms)\n", sequentialTimeFE);
			fprintf(out, "Sequential Time w/oDD+BS+PV : %.3f(ms)\n\n", sequentialTimeFENOPV);
		}
		
		// Print Speedups
		if(P > 1)
		{
			fprintf(out, "Parallel Time w/DD+BS  : %.3f(ms)\n", ddtime + fetime + bstime);
			fprintf(out, "Speedup       w/DD+BS  : %.3f\n\n", sequentialTimeDDBSFEPV / (ddtime + fetime + bstime));
			
			fprintf(out, "Parallel Time DD only     : %.3f(ms)\n", ddtime);
			fprintf(out, "Speedup       DD only     : %.3f\n\n", sequentialTimeDD / ddtime);
			
			fprintf(out, "Parallel Time BS only     : %.3f(ms)\n", bstime);
			fprintf(out, "Speedup       BS only     : %.3f\n\n", sequentialTimeBS / bstime);
			
			fprintf(out, "Parallel Time PV only     : %.3f(ms)\n", pvtime);
			fprintf(out, "Speedup       PV only     : %.3f\n\n", sequentialTimePV / pvtime);
			
			fprintf(out, "Parallel Time w/oDD+BS    : %.3f(ms)\n", fetime);
			fprintf(out, "Speedup       w/oDD+BS    : %.3f\n", sequentialTimeFE / fetime);
			
			fprintf(out, "Parallel Time w/oDD+BS+PV    : %.3f(ms)\n", fetime - pvtime);
			fprintf(out, "Speedup       w/oDD+BS+PV    : %.3f\n", (sequentialTimeFE -  sequentialTimePV) / (fetime - pvtime));
		}
		fprintf(out, "-------------------------------------------------------------------------------------------------------------\n\n");
		fclose(out);
		
		// Write aggregated results
		// In a csv file with columns
		// INPUT NAME, N, P, R, DISTRIBUTE_READ, TIME, SPEEDUP
		out = fopen("aggregatedResults.csv", "a");
		fprintf(out, "%s, %d, %d, %d, %d, %f, %f, %f, %f, %f, %f, ", inputFile, N, P, R, DISTRIBUTE_READ, 
			(ddtime + fetime + bstime), ddtime, bstime, pvtime, fetime, (fetime - pvtime));
		if(P > 1)
		{		
			fprintf(out, "%f, ", sequentialTimeDDBSFEPV / (ddtime + fetime + bstime));
			fprintf(out, "%f, ", sequentialTimeDD / ddtime);
			fprintf(out, "%f, ", sequentialTimeBS / bstime);
			fprintf(out, "%f, ", sequentialTimePV / pvtime);
			fprintf(out, "%f, ", sequentialTimeFE / fetime);
			fprintf(out, "%f\n", (sequentialTimeFE -  sequentialTimePV) / (fetime - pvtime));
		}
		else 
		{
			fprintf(out, "1.0, 1.0, 1.0, 1.0\n");
		}
		fclose(out);
	}
	
	if(OUTPUT_UX)
	{
		if(PID == 0)
		{
			// Write the solution vector and U matrix.
			char filename[32];
			sprintf(filename, "op_P_%d_N_%d_R_%d_%d_UX.txt", P, N, R, DISTRIBUTE_READ);
			FILE* out = fopen(filename, "w");
			fprintf(out, "P = %d, distributed_read = %d, N = %d, R = %d, input = %s\n", P, DISTRIBUTE_READ, N, R, inputFile);
			fprintf(out, "Checksum1(X) = %11.4e,    Checksum2(X) = %11.4e,    Checksum1(U) = %11.4e,    Checksum2(U) = %11.4e\n", cx1, cx2, cu1, cu2);
			fprintf(out, "---------------------------------------------------------------------------------------------------\n");
			fprintf(out, "Solution Vector:\n");
			outputSolution(out, X);
			fprintf(out, "---------------------------------------------------------------------------------------------------\n");
			fprintf(out, "U Matrix:\n");
			outputMatrix(out, U);
			fclose(out);
		}
	}
}
void AnisotropicElasticityTensor::calculateEntries(unsigned int /*qp*/)
{

  //form_r_matrix();
  //Form rotation matrix from Euler angles
  const Real phi1 = _euler_angle[0] * (M_PI/180.0);
  const Real phi = _euler_angle[1] * (M_PI/180.0);
  const Real phi2 = _euler_angle[2] * (M_PI/180.0);

  Real cp1 = cos(phi1);
  Real cp2 = cos(phi2);
  Real cp = cos(phi);

  Real sp1 = sin(phi1);
  Real sp2 = sin(phi2);
  Real sp = sin(phi);

  DenseMatrix<Real> R; // Rotational Matrix
  R(0,0) = cp1 * cp2 - sp1 * sp2 * cp;
  R(0,1) = sp1 * cp2 + cp1 * sp2 * cp;
  R(0,2) = sp2 * sp;
  R(1,0) = -cp1 * sp2 - sp1 * cp2 * cp;
  R(1,1) = -sp1 * sp2 + cp1 * cp2 * cp;
  R(1,2) = cp2 * sp;
  R(2,0) = sp1 * sp;
  R(2,1) = -cp1 * sp;
  R(2,2) = cp;

  //Initialize material Dt matrix
  DenseMatrix<Real> Dt; // 6 x 6 Material Matrix
  Dt(0,0) = Dt(1,1) = Dt(2,2) = _c11;
  Dt(0,1) = Dt(0,2) = Dt(1,0) = Dt(2,0) = Dt(1,2) = Dt(2,1) = _c12;
  Dt(3,3) = Dt(4,4) = Dt(5,5) = 2 * _c44;

  //Form Q = R dyadic R and Q transpose
  DenseMatrix<Real> Q, Qt; // Q = R (dyadic) R and Q transpose
  for (unsigned int i = 0; i < 3; i++)
    for (unsigned int j = 0; j < 3; j++)
      for (unsigned int k = 0; k < 3; k++)
        for (unsigned int l = 0; l < 3; l++)
          Q(((i*3)+k),((j*3)+l)) = R(i,j) * R(k,l);

   for (unsigned int p = 0; p < 9; p++)
    for (unsigned int q = 0; q < 9; q++)
      Qt(q,p) = Q(p,q);

   // Form two kinds of transformation matrix
   // TransD6toD9 transfroms Dt[6][6] to Dmat[9][9]
   // TransD9toD6 transforms Dmat[9][9] to Dt[6][6]
   DenseMatrix<Real> trans_d6_to_d9, transpose_trans_d6_to_d9;
   DenseMatrix<Real> trans_d9_to_d6, transpose_trans_d9_to_d6;
   Real sqrt2 = std::sqrt(2.0);
   Real a = 1/sqrt2;

   trans_d6_to_d9(0,0) = trans_d6_to_d9(4,1) =  trans_d6_to_d9(8,2) = 1.0;
   trans_d6_to_d9(1,3) = trans_d6_to_d9(3,3) = a;
   trans_d6_to_d9(2,4) = trans_d6_to_d9(6,4) = a;
   trans_d6_to_d9(5,5) = trans_d6_to_d9(7,5) = a;

   for (unsigned int i = 0; i < 9; i++)
     for (unsigned int j = 0; j < 6; j++)
       transpose_trans_d6_to_d9(j,i) = trans_d6_to_d9(i,j);

   trans_d9_to_d6(0,0) = trans_d9_to_d6(1,4) =  trans_d9_to_d6(2,8) = 1.0;
   trans_d9_to_d6(3,3) = trans_d9_to_d6(4,6) =  trans_d9_to_d6(5,7) = sqrt2;


   for (unsigned int i = 0; i < 6; i++)
     for (unsigned int j = 0; j < 9; j++)
       transpose_trans_d9_to_d6(j,i) =  trans_d9_to_d6(i,j);

   // The function makes use of TransD6toD9 matrix to transfrom Dt[6][6] to Dmat[9][9]
   // Dmat = T * Dt * TT

   DenseMatrix<Real> outputMatrix(9,6);

   outputMatrix = trans_d6_to_d9;
   outputMatrix.right_multiply(Dt);

   DenseMatrix<Real> Dmat; // 9 x 9 Material Matrix
   Dmat = outputMatrix;
   Dmat.right_multiply(transpose_trans_d6_to_d9);

  // The function makes use of Q matrix to rotate Dmat[9][9] to QDmat[9][9]
  // QDmat = QT * Dmat * Q

  DenseMatrix<Real> outputMatrix99(9,9);

  outputMatrix99 = Qt;
  outputMatrix99.right_multiply(Dmat);

  DenseMatrix<Real> QDmat; // Rotated Material Matrix
  QDmat = outputMatrix99;
  QDmat.right_multiply(Q);

  //Convert 9X9 matrix QDmat to 81 vector

  unsigned int count = 0;

  for (unsigned int j = 0; j < 9; j++)
    for (unsigned int i = 0; i < 9; i++)
  {
    _values[count] = QDmat(i,j);
    count = count + 1;
  }
}