| 1 |
// Copyright (C) 2009 Anders Logg |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// First added: 2009-09-15 |
| 19 |
// Last changed: 2012-12-12 |
| 20 |
// |
| 21 |
// Simple Poisson solver |
| 22 |
|
| 23 |
#include <cstdlib> |
| 24 |
#include <dolfin.h> |
| 25 |
#include "Poisson.h" |
| 26 |
|
| 27 |
using namespace dolfin; |
| 28 |
|
| 29 |
int main(int argc, char* argv[]) |
| 30 |
{ |
| 31 |
#ifdef HAS_PETSC |
| 32 |
|
| 33 |
parameters["linear_algebra_backend"] = "PETSc"; |
| 34 |
|
| 35 |
// Parse command-line arguments |
| 36 |
if (argc != 2) |
| 37 |
{ |
| 38 |
info("Usage: solve-poisson n"); |
| 39 |
return 1; |
| 40 |
} |
| 41 |
int n = atoi(argv[1]); |
| 42 |
|
| 43 |
// Create mesh and function space |
| 44 |
auto mesh = std::make_shared<UnitCubeMesh>(n, n, n); |
| 45 |
auto V = std::make_shared<const Poisson::FunctionSpace>(mesh); |
| 46 |
|
| 47 |
// MPI communicator |
| 48 |
const MPI_Comm comm = mesh->mpi_comm(); |
| 49 |
|
| 50 |
// Define boundary condition |
| 51 |
auto u0 = std::make_shared<const Constant>(0.0); |
| 52 |
auto boundary = std::make_shared<const DomainBoundary>(); |
| 53 |
DirichletBC bc(V, u0, boundary); |
| 54 |
|
| 55 |
// Define variational problem |
| 56 |
Poisson::BilinearForm a(V, V); |
| 57 |
Poisson::LinearForm L(V); |
| 58 |
auto f= std::make_shared<Constant>(1.0); |
| 59 |
L.f = f; |
| 60 |
Function u(V); |
| 61 |
|
| 62 |
// Prepare iterative solver |
| 63 |
const auto pc = std::make_shared<PETScPreconditioner>("petsc_amg"); |
| 64 |
PETScKrylovSolver solver("cg", pc); |
| 65 |
|
| 66 |
// Assemble matrix and vector, and apply Dirichlet boundary conditions |
| 67 |
Matrix A; |
| 68 |
Vector b; |
| 69 |
assemble(A, a); |
| 70 |
assemble(b, L); |
| 71 |
bc.apply(A, b); |
| 72 |
bc.apply(*u.vector()); |
| 73 |
|
| 74 |
// Solve linear system |
| 75 |
dolfin::MPI::barrier(comm); |
| 76 |
double t = time(); |
| 77 |
solver.solve(A, *u.vector(), b); |
| 78 |
dolfin::MPI::barrier(comm); |
| 79 |
t = time() - t; |
| 80 |
if (dolfin::MPI::rank(comm) == 0) |
| 81 |
info("TIME (first time): %.5g", t); |
| 82 |
|
| 83 |
// Solve linear system (preconditioner assuming same non-zero pattern) |
| 84 |
if (solver.parameters.has_key("preconditioner")) |
| 85 |
solver.parameters("preconditioner")["structure"] = "same_nonzero_pattern"; |
| 86 |
u.vector()->zero(); |
| 87 |
dolfin::MPI::barrier(comm); |
| 88 |
t = time(); |
| 89 |
solver.solve(A, *u.vector(), b); |
| 90 |
dolfin::MPI::barrier(comm); |
| 91 |
t = time() - t; |
| 92 |
if (dolfin::MPI::rank(comm) == 0) |
| 93 |
info("TIME (same nonzero pattern): %.5g", t); |
| 94 |
|
| 95 |
// Solve linear system (re-use preconditioner) |
| 96 |
if (solver.parameters.has_key("preconditioner")) |
| 97 |
solver.parameters("preconditioner")["structure"] = "same"; |
| 98 |
u.vector()->zero(); |
| 99 |
dolfin::MPI::barrier(comm); |
| 100 |
t = time(); |
| 101 |
solver.solve(A, *u.vector(), b); |
| 102 |
dolfin::MPI::barrier(comm); |
| 103 |
t = time() - t; |
| 104 |
if (dolfin::MPI::rank(comm) == 0) |
| 105 |
info("TIME (re-use preconditioner): %.5g", t); |
| 106 |
|
| 107 |
#else |
| 108 |
error("This benchmark requires PETSc."); |
| 109 |
#endif |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |