| 1 |
"""This demo program demonstrates how to manipulate (higher-order) mesh |
| 2 |
coordinates.""" |
| 3 |
|
| 4 |
# Copyright (C) 2016 Jan Blechta |
| 5 |
# |
| 6 |
# This file is part of DOLFIN. |
| 7 |
# |
| 8 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# DOLFIN is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
from dolfin import * |
| 22 |
import matplotlib.pyplot as plt |
| 23 |
|
| 24 |
|
| 25 |
# Create mesh |
| 26 |
comm = MPI.comm_world |
| 27 |
mesh = UnitDiscMesh.create(comm, 20, 2, 2) |
| 28 |
plt.figure() |
| 29 |
plot(mesh) |
| 30 |
|
| 31 |
# Fetch coordinate function |
| 32 |
C = FunctionSpace(mesh, mesh.ufl_coordinate_element()) |
| 33 |
c = Function(C) |
| 34 |
get_coordinates(c, mesh.geometry()) |
| 35 |
|
| 36 |
# Deform coordinates harmonically subject to BC |
| 37 |
u, v = TrialFunction(C), TestFunction(C) |
| 38 |
a = inner(grad(u), grad(v))*dx |
| 39 |
L = dot(Constant((0, 0)), v)*dx |
| 40 |
bc1 = DirichletBC(C, (-1, -1), "x[0] < -0.5") |
| 41 |
bc2 = DirichletBC(C, c, "x[0] >= -0.5") |
| 42 |
displacement = Function(C) |
| 43 |
solve(a == L, displacement, [bc1, bc2]) |
| 44 |
c_vec = c.vector() |
| 45 |
c_vec += displacement.vector() |
| 46 |
|
| 47 |
# Set coordinates |
| 48 |
set_coordinates(mesh.geometry(), c) |
| 49 |
plt.figure() |
| 50 |
plot(mesh) |
| 51 |
|
| 52 |
# We can create (cubic) mesh from function |
| 53 |
C3 = VectorFunctionSpace(mesh, "Lagrange", 4) |
| 54 |
c3 = interpolate(c, C3) |
| 55 |
mesh3 = create_mesh(c3) |
| 56 |
plt.figure() |
| 57 |
plot(mesh3) |
| 58 |
|
| 59 |
# Display plots |
| 60 |
plt.show() |