| 1 |
// Copyright (C) 2010 Kristian B. Oelgaard |
| 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: 2010-03-05 |
| 19 |
// Last changed: 2012-11-12 |
| 20 |
// |
| 21 |
// This demo program solves Poisson's equation |
| 22 |
// |
| 23 |
// - div grad u(x, y) = f(x, y) |
| 24 |
// |
| 25 |
// on the unit square with source f given by |
| 26 |
// |
| 27 |
// f(x, y) = 10*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) |
| 28 |
// |
| 29 |
// and boundary conditions given by |
| 30 |
// |
| 31 |
// u(x, y) = 0 for x = 0 or x = 1 |
| 32 |
// du/dn(x, y) = -sin(5*x) for y = 0 or y = 1 |
| 33 |
// |
| 34 |
// This demo is identical to the Poisson demo with the only difference that |
| 35 |
// the source and flux term is expressed using SpatialCoordinates in the |
| 36 |
// variational formulation. |
| 37 |
|
| 38 |
#include <dolfin.h> |
| 39 |
#include "SpatialCoordinates.h" |
| 40 |
|
| 41 |
using namespace dolfin; |
| 42 |
|
| 43 |
// Sub domain for Dirichlet boundary condition |
| 44 |
class DirichletBoundary : public SubDomain |
| 45 |
{ |
| 46 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 47 |
{ |
| 48 |
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
int main() |
| 53 |
{ |
| 54 |
// Create mesh and function space |
| 55 |
auto mesh = std::make_shared<UnitSquareMesh>(32, 32); |
| 56 |
auto V = std::make_shared<SpatialCoordinates::FunctionSpace>(mesh); |
| 57 |
|
| 58 |
// Define boundary condition |
| 59 |
auto u0 = std::make_shared<Constant>(0.0); |
| 60 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 61 |
auto bc = std::make_shared<DirichletBC>(V, u0, boundary); |
| 62 |
|
| 63 |
// Define variational problem |
| 64 |
auto a = std::make_shared<SpatialCoordinates::BilinearForm>(V, V); |
| 65 |
auto L = std::make_shared<SpatialCoordinates::LinearForm>(V); |
| 66 |
auto u = std::make_shared<Function>(V); |
| 67 |
std::vector<std::shared_ptr<const DirichletBC>> bcs = {bc}; |
| 68 |
auto problem = std::make_shared<LinearVariationalProblem>(a, L, u, bcs); |
| 69 |
|
| 70 |
// Compute solution |
| 71 |
LinearVariationalSolver solver(problem); |
| 72 |
solver.parameters["linear_solver"] = "iterative"; |
| 73 |
solver.solve(); |
| 74 |
|
| 75 |
// Save solution in VTK format |
| 76 |
File file("spatial-coordinates.pvd"); |
| 77 |
file << *u; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |