| 1 |
# Copyright (C) 2014-2017 Anders Logg |
| 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: 2014-04-07 |
| 19 |
# Last changed: 2017-05-17 |
| 20 |
# |
| 21 |
# This demo program illustrates multimesh quadrature on a pair |
| 22 |
# of overlapping meshes using red dots to mark positive quadature |
| 23 |
# weights and black dots to mark negative quadature weights. |
| 24 |
# |
| 25 |
# To create movies from the generated PNG files, enter the output |
| 26 |
# directory and run the following commmands: |
| 27 |
# |
| 28 |
# ffmpeg -i multimesh_quadrature_%04d.png multimesh_quadrature.mp4 |
| 29 |
# ffmpeg -i multimesh_quadrature_%04d.png multimesh_quadrature_compressed.mp4 |
| 30 |
|
| 31 |
from dolfin import * |
| 32 |
import os |
| 33 |
|
| 34 |
# Don't plot when DOLFIN_NOPLOT is set |
| 35 |
plot = os.environ.get("DOLFIN_NOPLOT") is None |
| 36 |
if plot: |
| 37 |
import pylab as pl |
| 38 |
|
| 39 |
# Colors for plotting |
| 40 |
red = "#ff3c00" |
| 41 |
green = "#59ce55" |
| 42 |
yellow = "#fff0aa" |
| 43 |
blue = "#b4d8e7" |
| 44 |
white = "#ffffff" |
| 45 |
black = "#000000" |
| 46 |
|
| 47 |
def plot_triangle(c, mesh, color=None, alpha_fill=None, alpha_line=None): |
| 48 |
if not plot: return |
| 49 |
cell = Cell(mesh, c) |
| 50 |
xy = cell.get_vertex_coordinates() |
| 51 |
x = [xy[0], xy[2], xy[4]] |
| 52 |
y = [xy[1], xy[3], xy[5]] |
| 53 |
if not color is None: pl.fill(x, y, color=color, alpha=alpha_fill) |
| 54 |
pl.plot(x + [x[0]], y + [y[0]], color='k', alpha=alpha_line) |
| 55 |
|
| 56 |
def plot_point_red(x, y): |
| 57 |
if not plot: return |
| 58 |
pl.plot(x, y, '.', markersize=5, color=red) |
| 59 |
|
| 60 |
def plot_point_black(x, y): |
| 61 |
if not plot: return |
| 62 |
pl.plot(x, y, '.', markersize=2, color=black) |
| 63 |
|
| 64 |
def clear_plot(): |
| 65 |
if not plot: return |
| 66 |
pl.clf() |
| 67 |
|
| 68 |
def save_plot(frame, compress): |
| 69 |
if not plot: return |
| 70 |
if not os.path.isdir("output"): os.makedirs("output") |
| 71 |
pl.axis("equal") |
| 72 |
pl.axis("off") |
| 73 |
c = "_compressed" if compress else "" |
| 74 |
pl.savefig("output/multimesh_quadrature%s_%.4d.png" % (c, frame), dpi=300) |
| 75 |
|
| 76 |
def show_plot(): |
| 77 |
if not plot: return |
| 78 |
pl.show() |
| 79 |
|
| 80 |
# Parameters |
| 81 |
N = 8 |
| 82 |
dv = 5 |
| 83 |
R = 0.6 |
| 84 |
num_frames = 3 |
| 85 |
|
| 86 |
# Uncomment to generate a longer movie |
| 87 |
#num_frames = 360 |
| 88 |
|
| 89 |
# Iterate over quadrature compression on/off |
| 90 |
for compress in [True, False]: |
| 91 |
print("Compression:", compress) |
| 92 |
# Create and load meshes |
| 93 |
mesh = RectangleMesh(Point(-R, -R), Point(R, R), N, N) |
| 94 |
propeller = Mesh("../propeller_2d_coarse.xml.gz") |
| 95 |
|
| 96 |
# Iterate over frames (rotations of the propeller) |
| 97 |
for frame in range(num_frames): |
| 98 |
print("Frame %d out of %d..." % (frame + 1, num_frames)) |
| 99 |
|
| 100 |
# Rotate propeller |
| 101 |
propeller.rotate(dv) |
| 102 |
|
| 103 |
# Build multimesh |
| 104 |
multimesh = MultiMesh() |
| 105 |
multimesh.parameters["compress_volume_quadrature"] = compress |
| 106 |
multimesh.add(mesh) |
| 107 |
multimesh.add(propeller) |
| 108 |
multimesh.build() |
| 109 |
|
| 110 |
# Extract data |
| 111 |
cut_cells = multimesh.cut_cells(0) |
| 112 |
uncut_cells = multimesh.uncut_cells(0) |
| 113 |
covered_cells = multimesh.covered_cells(0) |
| 114 |
|
| 115 |
# Clear plot |
| 116 |
clear_plot() |
| 117 |
|
| 118 |
# Plot cells in background mesh |
| 119 |
for c in cut_cells: |
| 120 |
plot_triangle(c, mesh, yellow) |
| 121 |
for c in uncut_cells: |
| 122 |
plot_triangle(c, mesh, blue) |
| 123 |
for c in covered_cells: |
| 124 |
plot_triangle(c, mesh, white, alpha_line=0.1) |
| 125 |
|
| 126 |
# Plot propeller mesh |
| 127 |
for c in range(propeller.num_cells()): |
| 128 |
plot_triangle(c, propeller, color=white, alpha_fill=0.25) |
| 129 |
|
| 130 |
# Plot quadrature points |
| 131 |
for c in cut_cells: |
| 132 |
points, weights = multimesh.quadrature_rules_cut_cells(0, c) |
| 133 |
for i in range(len(weights)): |
| 134 |
w = weights[i] |
| 135 |
x = points[2*i] |
| 136 |
y = points[2*i + 1] |
| 137 |
if w > 0: |
| 138 |
plot_point_red(x, y) |
| 139 |
else: |
| 140 |
plot_point_black(x, y) |
| 141 |
|
| 142 |
# Save plot |
| 143 |
save_plot(frame, compress) |
| 144 |
|
| 145 |
# Show last frame |
| 146 |
show_plot() |