| 1 |
// Copyright (C) 2012 Imperial College London and others. |
| 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 David Ham 2012 |
| 19 |
// |
| 20 |
// First added: 2012-12-04 |
| 21 |
// Last changed: 2012-12-04 |
| 22 |
// |
| 23 |
// This demo program solves Poisson's equation |
| 24 |
// |
| 25 |
// - div grad u(xi) = f(xi) |
| 26 |
// |
| 27 |
// on a unit interval embedded in 2D and rotated pi/4 radians |
| 28 |
// anticlockwise from the x axis, where xi is the distance along the |
| 29 |
// interval (ie the domain is 0<xi<1). The source f is given by |
| 30 |
// |
| 31 |
// f(xi) = 9*pi^2*sin(3*pi*xi) |
| 32 |
// |
| 33 |
// The boundary conditions are given by |
| 34 |
// |
| 35 |
// u(xi) = 0 for xi = 0 |
| 36 |
// du/dxi = 0 for xi = 1 |
| 37 |
|
| 38 |
#include <dolfin.h> |
| 39 |
#include "Poisson.h" |
| 40 |
|
| 41 |
using namespace dolfin; |
| 42 |
|
| 43 |
#define THETA DOLFIN_PI/4.0 |
| 44 |
|
| 45 |
// Map the point x back to the horizontal line |
| 46 |
double to_interval(const Array<double>& x) |
| 47 |
{ |
| 48 |
return (x[0]*cos(THETA) + x[1]*sin(THETA)); |
| 49 |
} |
| 50 |
|
| 51 |
// Rotate the mesh through theta |
| 52 |
void rotate(Mesh& mesh) |
| 53 |
{ |
| 54 |
std::vector<double>& x = mesh.coordinates(); |
| 55 |
double tmpx; |
| 56 |
for (std::size_t i = 0; i < mesh.num_vertices(); i++) |
| 57 |
{ |
| 58 |
tmpx = x[2*i]*cos(THETA) - x[2*i+1]*sin(THETA); |
| 59 |
|
| 60 |
x[2*i+1] = x[2*i]*sin(THETA) + x[2*i+1]*cos(THETA); |
| 61 |
x[2*i] = tmpx; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Subdomain to extract the bottom boundary of the mesh. |
| 66 |
class BottomEdge : public SubDomain |
| 67 |
{ |
| 68 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 69 |
{ return (std::abs(x[1]) < DOLFIN_EPS); } |
| 70 |
}; |
| 71 |
|
| 72 |
// Boundary condition |
| 73 |
class DirichletBoundary : public SubDomain |
| 74 |
{ |
| 75 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 76 |
{ return (std::abs(to_interval(x)) < DOLFIN_EPS); } |
| 77 |
}; |
| 78 |
|
| 79 |
// Source term |
| 80 |
class Source : public Expression |
| 81 |
{ |
| 82 |
public: |
| 83 |
|
| 84 |
void eval(Array<double>& values, const Array<double>& x) const |
| 85 |
{ values[0] = 9.0*DOLFIN_PI*DOLFIN_PI*sin(3.0*DOLFIN_PI*to_interval(x)); } |
| 86 |
|
| 87 |
}; |
| 88 |
|
| 89 |
// Neumann boundary condition |
| 90 |
class Flux : public Expression |
| 91 |
{ |
| 92 |
public: |
| 93 |
|
| 94 |
void eval(Array<double>& values, const Array<double>& x) const |
| 95 |
{ values[0] = 3.0*DOLFIN_PI*cos(3.0*DOLFIN_PI*to_interval(x)); } |
| 96 |
|
| 97 |
}; |
| 98 |
|
| 99 |
int main() |
| 100 |
{ |
| 101 |
if (dolfin::MPI::size(MPI_COMM_WORLD) > 1) |
| 102 |
{ |
| 103 |
std::cout << "This demo does not work in parallel" << std::endl; |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
// Create original square mesh |
| 108 |
UnitSquareMesh squaremesh(50, 2); |
| 109 |
|
| 110 |
// Grab the surface of the mesh |
| 111 |
BoundaryMesh boundarymesh(squaremesh, "exterior"); |
| 112 |
|
| 113 |
// The actual mesh is just the bottom. |
| 114 |
auto mesh = std::make_shared<SubMesh>(boundarymesh, BottomEdge()); |
| 115 |
|
| 116 |
// Rotate mesh coordinates. |
| 117 |
rotate(*mesh); |
| 118 |
|
| 119 |
// Create function space |
| 120 |
auto V = std::make_shared<Poisson::FunctionSpace>(mesh); |
| 121 |
|
| 122 |
// Set up BCs |
| 123 |
auto zero = std::make_shared<Constant>(0.0); |
| 124 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 125 |
DirichletBC bc(V, zero, boundary); |
| 126 |
|
| 127 |
// Create source and flux terms |
| 128 |
auto f = std::make_shared<Source>(); |
| 129 |
auto g = std::make_shared<Flux>(); |
| 130 |
|
| 131 |
// Define variational problem |
| 132 |
Poisson::BilinearForm a(V, V); |
| 133 |
Poisson::LinearForm L(V); |
| 134 |
L.f = f; |
| 135 |
L.g = g; |
| 136 |
|
| 137 |
// Solve PDE |
| 138 |
Function u(V); |
| 139 |
solve(a == L, u, bc); |
| 140 |
|
| 141 |
// Save solution in VTK format |
| 142 |
File file_u("poisson.pvd"); |
| 143 |
file_u << u; |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |