| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (C) 2016 Jan Blechta, Martin Alnæs |
| 3 |
# |
| 4 |
# This file is part of DOLFIN. |
| 5 |
# |
| 6 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# DOLFIN is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public License |
| 17 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
|
| 20 |
from dolfin import * |
| 21 |
import ufl_legacy as ufl |
| 22 |
import os |
| 23 |
|
| 24 |
exit(0) |
| 25 |
|
| 26 |
try: |
| 27 |
import matplotlib.pyplot as plt |
| 28 |
from mpl_toolkits.mplot3d import axes3d |
| 29 |
except ImportError: |
| 30 |
print("This demo requires matplotlib! Bye.") |
| 31 |
exit() |
| 32 |
|
| 33 |
|
| 34 |
rank = MPI.rank(MPI.comm_world) |
| 35 |
size = MPI.size(MPI.comm_world) |
| 36 |
suffix = "_r%s" % rank if size > 1 else "" |
| 37 |
|
| 38 |
|
| 39 |
def plot_alongside(*args, **kwargs): |
| 40 |
"""Plot supplied functions in single figure with common colorbar. |
| 41 |
It is users responsibility to supply 'range_min' and 'range_max' in kwargs. |
| 42 |
""" |
| 43 |
n = len(args) |
| 44 |
plt.figure(figsize=(4*n+2, 4)) |
| 45 |
projection = "3d" if kwargs.get("mode") == "warp" else None |
| 46 |
|
| 47 |
for i in range(n): |
| 48 |
plt.subplot(1, n, i+1, projection=projection) |
| 49 |
p = plot(args[i], **kwargs) |
| 50 |
|
| 51 |
plt.tight_layout() |
| 52 |
|
| 53 |
# Create colorbar |
| 54 |
plt.subplots_adjust(right=0.8) |
| 55 |
cbar_ax = plt.gcf().add_axes([0.85, 0.15, 0.05, 0.7]) |
| 56 |
plt.colorbar(p, cax=cbar_ax) |
| 57 |
|
| 58 |
|
| 59 |
def create_interval_mesh(vertices): |
| 60 |
"Given list of vertex coordinate tuples, build and return a mesh of intervals." |
| 61 |
gdim = len(vertices[0]) |
| 62 |
mesh = Mesh() |
| 63 |
me = MeshEditor() |
| 64 |
me.open(mesh, "interval", 1, gdim) |
| 65 |
|
| 66 |
# Add vertices to mesh |
| 67 |
nv = len(vertices) |
| 68 |
me.init_vertices(nv) |
| 69 |
for i, v in enumerate(vertices): |
| 70 |
me.add_vertex(i, *v) |
| 71 |
|
| 72 |
# Add cells to mesh |
| 73 |
me.init_cells(nv-1) |
| 74 |
for i in range(nv-1): |
| 75 |
c = (i, i+1) |
| 76 |
me.add_cell(i, *c) |
| 77 |
|
| 78 |
me.close() |
| 79 |
assert mesh.ordered() |
| 80 |
return mesh |
| 81 |
|
| 82 |
|
| 83 |
def interval_mesh(gdim, n): |
| 84 |
us = [i/float(n-1) for i in range(n)] |
| 85 |
vertices = [(cos(4.0*DOLFIN_PI*u), sin(4.0*DOLFIN_PI*u), 2.0*u)[-gdim:] for u in us] |
| 86 |
return create_interval_mesh(vertices) |
| 87 |
|
| 88 |
|
| 89 |
def plot_1d_meshes(): |
| 90 |
# FIXME: This passes fine in parallel although it's not obvious what does it do |
| 91 |
plt.figure() |
| 92 |
plot(interval_mesh(1, 30)) |
| 93 |
plt.savefig("mesh_1d%s.png" % suffix) |
| 94 |
|
| 95 |
plt.figure() |
| 96 |
plot(interval_mesh(2, 100)) |
| 97 |
plt.savefig("mesh_2d%s.png" % suffix) |
| 98 |
|
| 99 |
plt.figure() |
| 100 |
plot(interval_mesh(3, 100)) |
| 101 |
plt.savefig("mesh_3d%s.png" % suffix) |
| 102 |
|
| 103 |
|
| 104 |
def plot_functions(): |
| 105 |
mesh = UnitSquareMesh(8, 8, 'crossed') |
| 106 |
P1 = FunctionSpace(mesh, "Lagrange", 1) |
| 107 |
u = interpolate(Expression("x[0]*x[0] + x[1]*x[1]*(x[1]-0.5)", degree=3), P1) |
| 108 |
v = interpolate(Expression("1.0+x[0]*x[0]", degree=2), P1) |
| 109 |
|
| 110 |
# Get common range |
| 111 |
r = {"range_min": min(u.vector().min(), v.vector().min()), |
| 112 |
"range_max": max(u.vector().max(), v.vector().max())} |
| 113 |
|
| 114 |
plot_alongside(u, v, **r) |
| 115 |
plot_alongside(u, v, mode='color', **r) |
| 116 |
plt.savefig("color_plot%s.pdf" % suffix) |
| 117 |
plot_alongside(u, v, mode='warp', **r) |
| 118 |
plt.savefig("warp_plot%s.pdf" % suffix) |
| 119 |
plot_alongside(u, v, v, mode='warp', **r) |
| 120 |
|
| 121 |
|
| 122 |
def main(argv=None): |
| 123 |
plot_1d_meshes() |
| 124 |
plot_functions() |
| 125 |
if os.environ.get("DOLFIN_NOPLOT", "0") == "0": |
| 126 |
plt.show() |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == '__main__': |
| 130 |
import sys |
| 131 |
main(sys.argv) |