| 1 |
"""This demo program solves Poisson's equation |
| 2 |
|
| 3 |
- div C grad u(x, y) = f(x, y) |
| 4 |
|
| 5 |
on the unit square with source f given by |
| 6 |
|
| 7 |
f(x, y) = 10*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) |
| 8 |
|
| 9 |
and boundary conditions given by |
| 10 |
|
| 11 |
u(x, y) = 0 for x = 0 or x = 1 |
| 12 |
du/dn(x, y) = 0 for y = 0 or y = 1 |
| 13 |
|
| 14 |
The conductivity C is a symmetric 2 x 2 matrix which |
| 15 |
varies throughout the domain. In the left part of the |
| 16 |
domain, the conductivity is |
| 17 |
|
| 18 |
C = ((1, 0.3), (0.3, 2)) |
| 19 |
|
| 20 |
and in the right part it is |
| 21 |
|
| 22 |
C = ((3, 0.5), (0.5, 4)) |
| 23 |
|
| 24 |
The data files where these values are stored are generated |
| 25 |
by the program generate_data.py |
| 26 |
|
| 27 |
This demo is dedicated to BF and Marius... ;-) |
| 28 |
""" |
| 29 |
|
| 30 |
# Copyright (C) 2009-2011 Anders Logg |
| 31 |
# |
| 32 |
# This file is part of DOLFIN. |
| 33 |
# |
| 34 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 35 |
# it under the terms of the GNU Lesser General Public License as published by |
| 36 |
# the Free Software Foundation, either version 3 of the License, or |
| 37 |
# (at your option) any later version. |
| 38 |
# |
| 39 |
# DOLFIN is distributed in the hope that it will be useful, |
| 40 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 41 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 42 |
# GNU Lesser General Public License for more details. |
| 43 |
# |
| 44 |
# You should have received a copy of the GNU Lesser General Public License |
| 45 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 46 |
# |
| 47 |
# First added: 2009-12-16 |
| 48 |
# Last changed: 2011-06-28 |
| 49 |
# Begin demo |
| 50 |
|
| 51 |
from dolfin import * |
| 52 |
|
| 53 |
# Read mesh from file and create function space |
| 54 |
mesh = Mesh("../unitsquare_32_32.xml.gz") |
| 55 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 56 |
|
| 57 |
# Define Dirichlet boundary (x = 0 or x = 1) |
| 58 |
def boundary(x): |
| 59 |
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS |
| 60 |
|
| 61 |
# Define boundary condition |
| 62 |
u0 = Constant(0.0) |
| 63 |
bc = DirichletBC(V, u0, boundary) |
| 64 |
|
| 65 |
# Define conductivity components as MeshFunctions |
| 66 |
c00 = MeshFunction("double", mesh, "../unitsquare_32_32_c00.xml.gz") |
| 67 |
c01 = MeshFunction("double", mesh, "../unitsquare_32_32_c01.xml.gz") |
| 68 |
c11 = MeshFunction("double", mesh, "../unitsquare_32_32_c11.xml.gz") |
| 69 |
|
| 70 |
# Code for C++ evaluation of conductivity |
| 71 |
conductivity_code = """ |
| 72 |
|
| 73 |
#include <pybind11/pybind11.h> |
| 74 |
#include <pybind11/eigen.h> |
| 75 |
namespace py = pybind11; |
| 76 |
|
| 77 |
#include <dolfin/function/Expression.h> |
| 78 |
#include <dolfin/mesh/MeshFunction.h> |
| 79 |
|
| 80 |
class Conductivity : public dolfin::Expression |
| 81 |
{ |
| 82 |
public: |
| 83 |
|
| 84 |
// Create expression with 3 components |
| 85 |
Conductivity() : dolfin::Expression(3) {} |
| 86 |
|
| 87 |
// Function for evaluating expression on each cell |
| 88 |
void eval(Eigen::Ref<Eigen::VectorXd> values, Eigen::Ref<const Eigen::VectorXd> x, const ufc::cell& cell) const override |
| 89 |
{ |
| 90 |
const uint cell_index = cell.index; |
| 91 |
values[0] = (*c00)[cell_index]; |
| 92 |
values[1] = (*c01)[cell_index]; |
| 93 |
values[2] = (*c11)[cell_index]; |
| 94 |
} |
| 95 |
|
| 96 |
// The data stored in mesh functions |
| 97 |
std::shared_ptr<dolfin::MeshFunction<double>> c00; |
| 98 |
std::shared_ptr<dolfin::MeshFunction<double>> c01; |
| 99 |
std::shared_ptr<dolfin::MeshFunction<double>> c11; |
| 100 |
|
| 101 |
}; |
| 102 |
|
| 103 |
PYBIND11_MODULE(SIGNATURE, m) |
| 104 |
{ |
| 105 |
py::class_<Conductivity, std::shared_ptr<Conductivity>, dolfin::Expression> |
| 106 |
(m, "Conductivity") |
| 107 |
.def(py::init<>()) |
| 108 |
.def_readwrite("c00", &Conductivity::c00) |
| 109 |
.def_readwrite("c01", &Conductivity::c01) |
| 110 |
.def_readwrite("c11", &Conductivity::c11); |
| 111 |
} |
| 112 |
|
| 113 |
""" |
| 114 |
|
| 115 |
c = CompiledExpression(compile_cpp_code(conductivity_code).Conductivity(), |
| 116 |
c00=c00, c01=c01, c11=c11, degree=0) |
| 117 |
|
| 118 |
C = as_matrix(((c[0], c[1]), (c[1], c[2]))) |
| 119 |
|
| 120 |
# Define variational problem |
| 121 |
u = TrialFunction(V) |
| 122 |
v = TestFunction(V) |
| 123 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2) |
| 124 |
a = inner(C*grad(u), grad(v))*dx |
| 125 |
L = f*v*dx |
| 126 |
|
| 127 |
# Compute solution |
| 128 |
u = Function(V) |
| 129 |
solve(a == L, u, bc) |
| 130 |
|
| 131 |
# Save solution in VTK format |
| 132 |
file = File("poisson.pvd") |
| 133 |
file << u |
| 134 |
|
| 135 |
# Plot solution |
| 136 |
import matplotlib.pyplot as plt |
| 137 |
plot(u) |
| 138 |
plt.show() |