| 1 |
"This demo program demonstrates various algorithms for mesh smoothing." |
| 2 |
|
| 3 |
# Copyright (C) 2010, 2015 Anders Logg |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# First added: 2010-03-02 |
| 21 |
# Last changed: 2010-05-11 |
| 22 |
|
| 23 |
from dolfin import * |
| 24 |
import matplotlib.pyplot as plt |
| 25 |
|
| 26 |
|
| 27 |
parameters["refinement_algorithm"] = "plaza" |
| 28 |
|
| 29 |
# Create rectangular mesh |
| 30 |
mesh = RectangleMesh(Point(0.0, 0.0), Point(2.4, 0.4), 60, 10) |
| 31 |
|
| 32 |
# Define a circular hole |
| 33 |
center = Point(0.2, 0.2) |
| 34 |
radius = 0.05 |
| 35 |
class Hole(SubDomain): |
| 36 |
|
| 37 |
def inside(self, x, on_boundary): |
| 38 |
r = sqrt((x[0] - center[0])**2 + (x[1] - center[0])**2) |
| 39 |
return r < 1.5*radius # slightly larger |
| 40 |
|
| 41 |
def snap(self, x): |
| 42 |
r = sqrt((x[0] - center[0])**2 + (x[1] - center[1])**2) |
| 43 |
if r < 1.5*radius: |
| 44 |
x[0] = center[0] + (radius / r)*(x[0] - center[0]) |
| 45 |
x[1] = center[1] + (radius / r)*(x[1] - center[1]) |
| 46 |
|
| 47 |
# Mark hole and extract submesh |
| 48 |
hole = Hole() |
| 49 |
sub_domains = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 50 |
sub_domains.set_all(0) |
| 51 |
hole.mark(sub_domains, 1) |
| 52 |
mesh = SubMesh(mesh, sub_domains, 0) |
| 53 |
mesh.snap_boundary(hole) |
| 54 |
|
| 55 |
# Refine and snap mesh |
| 56 |
plt.figure() |
| 57 |
plot(mesh, title="Mesh 0") |
| 58 |
num_refinements = 3 |
| 59 |
for i in range(num_refinements): |
| 60 |
|
| 61 |
# Mark cells for refinement |
| 62 |
markers = MeshFunction("bool", mesh, mesh.topology().dim()) |
| 63 |
markers.set_all(False) |
| 64 |
for cell in cells(mesh): |
| 65 |
if cell.midpoint().distance(center) < 2*radius: |
| 66 |
markers[cell.index()] = True |
| 67 |
|
| 68 |
# Refine mesh |
| 69 |
mesh = refine(mesh, markers) |
| 70 |
|
| 71 |
# Snap boundary |
| 72 |
mesh.snap_boundary(hole) |
| 73 |
|
| 74 |
# Plot mesh |
| 75 |
plt.figure() |
| 76 |
plot(mesh, title=("Mesh %d" % (i + 1))) |
| 77 |
|
| 78 |
plt.show() |