Exemplo n.º 1
1
int main(int, char**)
{
  cout.precision(3);
    int n = 10000;
  VectorXd x(n), b(n);
  SparseMatrix<double> A(n,n);
  /* ... fill A and b ... */ 
  BiCGSTAB<SparseMatrix<double> > solver;
  solver.compute(A);
  x = solver.solve(b);
  std::cout << "#iterations:     " << solver.iterations() << std::endl;
  std::cout << "estimated error: " << solver.error()      << std::endl;
  /* ... update b ... */
  x = solver.solve(b); // solve again
  return 0;
}
Exemplo n.º 2
-1
int main() {
  unsigned int end_step = END_STEP;
  double v        = V,
         h        = 1.0 / (N + 1),
         dt       = MU * h * h / v,
         c        = 0.5;

  // Initialize the temperature vector and fill with the square wave
  VectorXd u = VectorXd::Zero(N * N);  
  
  fourier2DSquare (u);
  
  double mu = v * dt / (h * h);
  if (mu > 0.5) 
    cerr << "Warning! Mu = " << mu << "; Solution may contain unphysical oscillations!" << endl;
  MatrixXd BBlock = MatrixXd::Zero (N, N);
  BBlock.diagonal (0) = VectorXd::Constant (N, -4 * mu);
  BBlock.diagonal (1) = BBlock.diagonal (-1) = VectorXd::Constant (N - 1, mu);
  MatrixXd A = MatrixXd::Zero(N * N, N * N);
  A.block<N, N>(0, 0) = BBlock;
  for (unsigned int i = 0; i < N - 1; ++i) {
    A.block<N, N>(N * (i + 1), N * (i + 1)) = BBlock;
    A.block<N, N>(N * (i + 1), N * i) = A.block<N, N>(N * i, N * (i + 1)) =
      MatrixXd::Identity(N, N) * mu;
  }

  displayField(u, 0, c);

  BiCGSTAB<MatrixXd> solver;

  for (unsigned int timestep = 1; timestep < end_step; ++timestep) {
    c = 0.5;
    VectorXd rhs = (MatrixXd::Identity(N * N, N * N) + c * A) * u;
    MatrixXd B   = (MatrixXd::Identity(N * N, N * N) - (1 - c) * A);
    solver.compute(B);
    u = solver.solve(rhs);
    cerr << "residual: " << (B * u - rhs).norm() << endl;

    displayField(u, timestep, c);
  }

  return 0;
}