| 1 |
// Copyright (C) 2006-2007 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 |
// Modified by Garth N. Wells, 2008. |
| 19 |
// |
| 20 |
// First added: 2006-02-07 |
| 21 |
// Last changed: 2012-11-12 |
| 22 |
// |
| 23 |
// This demo program solves Poisson's equation, |
| 24 |
// |
| 25 |
// - div grad u(x, y) = f(x, y) |
| 26 |
// |
| 27 |
// on the unit square with source f given by |
| 28 |
// |
| 29 |
// f(x, y) = 500*exp(-((x - 0.5)^2 + (y - 0.5)^2)/0.02) |
| 30 |
// |
| 31 |
// and boundary conditions given by |
| 32 |
// |
| 33 |
// u(x, y) = 0 for x = 0, |
| 34 |
// du/dn(x, y) = 25 cos(5 pi y) for x = 1, |
| 35 |
// du/dn(x, y) = 0 otherwise. |
| 36 |
|
| 37 |
#include <dolfin.h> |
| 38 |
#include "Poisson.h" |
| 39 |
|
| 40 |
using namespace dolfin; |
| 41 |
|
| 42 |
int main() |
| 43 |
{ |
| 44 |
// Source term |
| 45 |
class Source : public Expression |
| 46 |
{ |
| 47 |
public: |
| 48 |
|
| 49 |
Source() : Expression() {} |
| 50 |
|
| 51 |
void eval(Array<double>& values, const Array<double>& x) const |
| 52 |
{ |
| 53 |
double dx = x[0] - 0.5; |
| 54 |
double dy = x[1] - 0.5; |
| 55 |
values[0] = 500.0*exp(-(dx*dx + dy*dy)/0.02); |
| 56 |
} |
| 57 |
|
| 58 |
}; |
| 59 |
|
| 60 |
// Neumann boundary condition |
| 61 |
class Flux : public Expression |
| 62 |
{ |
| 63 |
public: |
| 64 |
|
| 65 |
Flux() : Expression() {} |
| 66 |
|
| 67 |
void eval(Array<double>& values, const Array<double>& x) const |
| 68 |
{ |
| 69 |
if (x[0] > (1.0 - DOLFIN_EPS)) |
| 70 |
values[0] = 25.0*cos(5.0*DOLFIN_PI*x[1]); |
| 71 |
else |
| 72 |
values[0] = 0.0; |
| 73 |
} |
| 74 |
|
| 75 |
}; |
| 76 |
|
| 77 |
// Sub domain for Dirichlet boundary condition |
| 78 |
class DirichletBoundary : public SubDomain |
| 79 |
{ |
| 80 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 81 |
{ |
| 82 |
return x[0] < DOLFIN_EPS; |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
// Create mesh |
| 87 |
auto mesh = std::make_shared<UnitSquareMesh>(300, 300); |
| 88 |
|
| 89 |
// Create functions |
| 90 |
auto f = std::make_shared<Source>(); |
| 91 |
auto g = std::make_shared<Flux>(); |
| 92 |
|
| 93 |
// Define forms and attach functions |
| 94 |
auto V = std::make_shared<Poisson::FunctionSpace>(mesh); |
| 95 |
Poisson::BilinearForm a(V, V); |
| 96 |
Poisson::LinearForm L(V); |
| 97 |
L.f = f; L.g = g; |
| 98 |
|
| 99 |
// Create boundary condition |
| 100 |
auto u0 = std::make_shared<Constant>(0.0); |
| 101 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 102 |
auto bc = std::make_shared<DirichletBC>(V, u0, boundary); |
| 103 |
|
| 104 |
// Create function |
| 105 |
Function u(V); |
| 106 |
|
| 107 |
// Create table |
| 108 |
Table table("Assembly and application of bcs"); |
| 109 |
|
| 110 |
// Matrix and vector to assemble |
| 111 |
Matrix A; |
| 112 |
Vector b; |
| 113 |
|
| 114 |
// Assemble A and b separately |
| 115 |
tic(); |
| 116 |
assemble(A, a); |
| 117 |
assemble(b, L); |
| 118 |
bc->apply(A, b); |
| 119 |
table("Standard", "Assembly time") = toc(); |
| 120 |
|
| 121 |
// Assemble A and b together |
| 122 |
tic(); |
| 123 |
assemble_system(A, b, a, L, {bc}); |
| 124 |
table("Symmetric", "Assembly time") = toc(); |
| 125 |
|
| 126 |
// Display summary |
| 127 |
info(table); |
| 128 |
|
| 129 |
// Solve system |
| 130 |
LUSolver solver; |
| 131 |
solver.solve(A, *u.vector(), b); |
| 132 |
|
| 133 |
// Save solution in VTK format |
| 134 |
File file("poisson.pvd"); |
| 135 |
file << u; |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |