Пример #1
0
double PDECond::Passo(){
  for(int i=1; i<X-1;i++){
    for(int j=1; j<Y-1;j++){
      if(M == mSOR)
	succ[i][j] = SOR(i,j);
      else if(M == mGaussSeidel)
	succ[i][j] = GaussSeidel(i,j);
      else
	succ[i][j] = Jacobi(i,j);
    }
  }
  return getErr();//ritorna l'errore la precisione e` decisa dall'utente
}
Пример #2
0
int main(int argc, char** argv)
{
  int i, j, N, flag;
  Matrix A=NULL, Q=NULL;
  Vector b, grid, e, lambda=NULL;
  double time, sum, h, tol=1e-4;

  if (argc < 3) {
    printf("need two parameters, N and flag [and tolerance]\n");
    printf(" - N is the problem size (in each direction\n");
    printf(" - flag = 1  -> Dense LU\n");
    printf(" - flag = 2  -> Dense Cholesky\n");
    printf(" - flag = 3  -> Full Gauss-Jacobi iterations\n");
    printf(" - flag = 4  -> Full Gauss-Jacobi iterations using BLAS\n");
    printf(" - flag = 5  -> Full Gauss-Seidel iterations\n");
    printf(" - flag = 6  -> Full Gauss-Seidel iterations using BLAS\n");
    printf(" - flag = 7  -> Full CG iterations\n");
    printf(" - flag = 8  -> Matrix-less Gauss-Jacobi iterations\n");
    printf(" - flag = 9  -> Matrix-less Gauss-Seidel iterations\n");
    printf(" - flag = 10 -> Matrix-less Red-Black Gauss-Seidel iterations\n");
    printf(" - flag = 11 -> Diagonalization\n");
    printf(" - flag = 12 -> Diagonalization - FST\n");
    printf(" - flag = 13 -> Matrix-less CG iterations\n");
    return 1;
  }
  N=atoi(argv[1]);
  flag=atoi(argv[2]);
  if (argc > 3)
    tol = atof(argv[3]);
  if (N < 0) {
    printf("invalid problem size given\n");
    return 2;
  }

  if (flag < 0 || flag > 13) {
    printf("invalid flag given\n");
    return 3;
  }

  if (flag == 10 && (N-1)%2 != 0) {
    printf("need an even size for red-black iterations\n");
    return 4;
  }
  if (flag == 12 && (N & (N-1)) != 0) {
    printf("need a power-of-two for fst-based diagonalization\n");
    return 5;
  }

  h = 1.0/N;

  grid = equidistantMesh(0.0, 1.0, N);
  b = createVector(N-1);
  e = createVector(N-1);
  evalMeshInternal(b, grid, source);
  evalMeshInternal(e, grid, exact);
  scaleVector(b, pow(h, 2));
  axpy(b, e, alpha);

  if (flag < 8) {
    A = createMatrix(N-1,N-1);
    diag(A, -1, -1.0);
    diag(A, 0, 2.0+alpha);
    diag(A, 1, -1.0);
  }

  if (flag >= 11 && flag < 13)
    lambda = generateEigenValuesP1D(N-1);
  if (flag == 11)
    Q = generateEigenMatrixP1D(N-1);

  time = WallTime();

  if (flag == 1) {
    int* ipiv=NULL;
    lusolve(A, b, &ipiv);
    free(ipiv);
  } else if (flag == 2)
    llsolve(A,b,0);
  else if (flag == 3)
    printf("Gauss-Jacobi used %i iterations\n",
           GaussJacobi(A, b, tol, 10000000));
  else if (flag == 4)
    printf("Gauss-Jacobi used %i iterations\n",
           GaussJacobiBlas(A, b, tol, 10000000));
  else if (flag == 5)
    printf("Gauss-Seidel used %i iterations\n",
           GaussSeidel(A, b, tol, 10000000));
  else if (flag == 6)
    printf("Gauss-Seidel used %i iterations\n",
           GaussSeidelBlas(A, b, tol, 10000000));
  else if (flag == 7)
    printf("CG used %i iterations\n", cg(A, b, 1e-8));
  else if (flag == 8)
    printf("Gauss-Jacobi used %i iterations\n",
           GaussJacobiPoisson1D(b, tol, 10000000));
  else if (flag == 9)
    printf("Gauss-Jacobi used %i iterations\n",
           GaussSeidelPoisson1D(b, tol, 10000000));
  else if (flag == 10)
    printf("Gauss-Jacobi used %i iterations\n",
           GaussSeidelPoisson1Drb(b, tol, 10000000));
  else if (flag == 11)
           DiagonalizationPoisson1D(b,lambda,Q);
  else if (flag == 12)
           DiagonalizationPoisson1Dfst(b,lambda);
  else if (flag == 13)
    printf("CG used %i iterations\n", cgMatrixFree(Poisson1D, b, tol));

  printf("elapsed: %f\n", WallTime()-time);

  evalMeshInternal(e, grid, exact);
  axpy(b,e,-1.0);

  printf("max error: %e\n", maxNorm(b));
  
  if (A)
    freeMatrix(A);
  if (Q)
    freeMatrix(Q);
  freeVector(grid);
  freeVector(b);
  freeVector(e);
  if (lambda)
    freeVector(lambda);
  return 0;
}
int main(int argc, char* argv[])
{
	if (argc != 4) {
		std::cout << "Missing parameters. Please run as: \n"
				  << "  ./project-01-solution <npoints> <tol> <maxiter> <storage> with:\n"
				  << "\n<npoints> the number of grid points on the side of the"
				  << "regular square grid\n"
				  << "\n<tol> the desired accuracy for the CG solver\n"
				  << "\n<maxiter> the maximum number of iterations\n"
				  << std::endl;
		return 1;
	}

	// Number of points along one side of the square regular grid
	unsigned long nx = atoi(argv[1]);
	// Tolerance for the CG solver
	Scalar tol = static_cast<Scalar>(atof(argv[2]));
	// Maximum number of CG iterations to perform
	int maxIter = atoi(argv[3]);

	// Timer object
	WallClock timer;
	timer.start();

	/*
	 * Number of DOF (N) is equal to just the interior grid points, because of
	 * the Dirichlet boundary conditions.
	 */
	unsigned long N = (nx - 2) * (nx - 2);

	// Right hand side vector
	Vector<Scalar> b(N);
	// Dirichlet boundary conditions on the lower side of the square:
	//    u(x,0) = 1;
	// The static casts are needed to prevent some warnings (-Wconversion)
	Scalar h = static_cast<Scalar>(1.0 / static_cast<Scalar>(nx - 1));
	Scalar h2 = h * h;
	for (unsigned long i = 0; i < nx - 2; ++i) {
		b[i] = static_cast<Scalar>(1.0 / h2);
	}

	// Solution vector
	Vector<Scalar> u(N);

	// Fill the system matrix A
	// The Laplacian matrix generator needs a pointer to the base class
	AbstractMatrix<Scalar>* A;
	Laplacian2DMatrixGenerator(A, nx, "dense");

	// Solve the system Ax = b;
	int numIter = GaussSeidel(*A, b, u, tol, maxIter);

	timer.stop();

	std::cout << "Solution reached after " << numIter << " iterations:\n";

	u.Print();

	std::cout << "Total execution time is: "
			  << timer.elapsedTime() << std::endl;

	delete A;

    return 0;
}