| 1 |
# very rough demo to test out ghost cells |
| 2 |
# run with mpirun |
| 3 |
# |
| 4 |
|
| 5 |
from dolfin import * |
| 6 |
import numpy as np |
| 7 |
import sys, os |
| 8 |
|
| 9 |
try: |
| 10 |
import matplotlib.pyplot as plt |
| 11 |
from matplotlib.collections import PolyCollection |
| 12 |
except ImportError: |
| 13 |
print("This demo requires matplotlib! Bye.") |
| 14 |
exit() |
| 15 |
|
| 16 |
parameters["ghost_mode"] = "shared_vertex" |
| 17 |
#parameters["ghost_mode"] = "shared_facet" |
| 18 |
#parameters["ghost_mode"] = "None" |
| 19 |
parameters["reorder_cells_gps"] = True |
| 20 |
parameters["reorder_vertices_gps"] = True |
| 21 |
|
| 22 |
n = 0 |
| 23 |
|
| 24 |
if(len(sys.argv) == 2): |
| 25 |
try: |
| 26 |
n = int(sys.argv[1]) |
| 27 |
except: |
| 28 |
n = 0 |
| 29 |
|
| 30 |
if(MPI.size(MPI.comm_world) == 1): |
| 31 |
print("Only works with MPI") |
| 32 |
quit() |
| 33 |
|
| 34 |
#parameters["mesh_partitioner"] = "ParMETIS" |
| 35 |
|
| 36 |
mesh = UnitSquareMesh(8, 8) |
| 37 |
# mesh = refine(M) |
| 38 |
|
| 39 |
shared_vertices = np.fromiter(mesh.topology().shared_entities(0).keys(), dtype='uintc') |
| 40 |
shared_cells = mesh.topology().shared_entities(mesh.topology().dim()) |
| 41 |
|
| 42 |
num_regular_vertices = mesh.topology().ghost_offset(0) |
| 43 |
|
| 44 |
ghost_vertices = np.arange(num_regular_vertices, mesh.topology().size(0)) |
| 45 |
|
| 46 |
verts_note = [] |
| 47 |
if (n == 0): |
| 48 |
for k,val in mesh.topology().shared_entities(0).items(): |
| 49 |
vtx = Vertex(mesh, k) |
| 50 |
verts_note.append( (vtx.point().x(), vtx.point().y(), " "+str(val)) ) |
| 51 |
elif (n == 1): |
| 52 |
for i in range(mesh.num_vertices()): |
| 53 |
vtx = Vertex(mesh, i) |
| 54 |
val = vtx.global_index() |
| 55 |
verts_note.append( (vtx.point().x(), vtx.point().y(), " "+str(val)) ) |
| 56 |
else: |
| 57 |
for i in range(mesh.num_vertices()): |
| 58 |
vtx = Vertex(mesh, i) |
| 59 |
val = vtx.index() |
| 60 |
verts_note.append( (vtx.point().x(), vtx.point().y(), " "+str(val)) ) |
| 61 |
|
| 62 |
x,y = mesh.coordinates().transpose() |
| 63 |
|
| 64 |
rank = MPI.rank(mesh.mpi_comm()) |
| 65 |
|
| 66 |
cell_ownership = np.ones(mesh.num_cells(),dtype='int')*rank |
| 67 |
cell_owner = mesh.topology().cell_owner() |
| 68 |
if len(cell_owner) > 0 : |
| 69 |
cell_ownership[-len(cell_owner):] = cell_owner |
| 70 |
|
| 71 |
cells_store=[] |
| 72 |
cells_note=[] |
| 73 |
colors=[] |
| 74 |
cmap=['red', 'green', 'yellow', 'purple', 'pink', 'grey', 'blue', 'brown'] |
| 75 |
|
| 76 |
idx = 0 |
| 77 |
for c in cells(mesh, "all"): |
| 78 |
xc=[] |
| 79 |
yc=[] |
| 80 |
for v in vertices(c): |
| 81 |
xc.append(v.point().x()) |
| 82 |
yc.append(v.point().y()) |
| 83 |
xavg = c.midpoint().x() |
| 84 |
yavg = c.midpoint().y() |
| 85 |
cell_str=str(c.index()) |
| 86 |
# if c.index() in shared_cells.keys(): |
| 87 |
# cell_str = str(shared_cells[c.index()]) |
| 88 |
# else: |
| 89 |
# cell_str = str(c.index()) |
| 90 |
cells_note.append((xavg, yavg, cell_str)) |
| 91 |
cells_store.append(list(zip(xc,yc))) |
| 92 |
|
| 93 |
colors.append(cmap[cell_ownership[c.index()]]) |
| 94 |
idx += 1 |
| 95 |
|
| 96 |
num_regular_facets = mesh.topology().ghost_offset(1) |
| 97 |
facet_note = [] |
| 98 |
shared_facets = mesh.topology().shared_entities(1) |
| 99 |
for f in facets(mesh, "all"): |
| 100 |
if (f.num_global_entities(2) == 2): |
| 101 |
color='#ffff88' |
| 102 |
else: |
| 103 |
color='#ff88ff' |
| 104 |
if (not f.is_ghost()): |
| 105 |
if (f.num_global_entities(2) == 2): |
| 106 |
color='#ffff00' |
| 107 |
else: |
| 108 |
color='#ff00ff' |
| 109 |
|
| 110 |
if (n < 3): |
| 111 |
facet_note.append((f.midpoint().x(), f.midpoint().y(), f.global_index(), color)) |
| 112 |
elif (n == 3): |
| 113 |
facet_note.append((f.midpoint().x(), f.midpoint().y(), f.index(), color)) |
| 114 |
else: |
| 115 |
if (f.index() in shared_facets.keys()): |
| 116 |
facet_note.append((f.midpoint().x(), f.midpoint().y(), shared_facets[f.index()], color)) |
| 117 |
|
| 118 |
fig, ax = plt.subplots() |
| 119 |
|
| 120 |
# Make the collection and add it to the plot. |
| 121 |
coll = PolyCollection(cells_store, facecolors=colors, edgecolors='#cccccc') |
| 122 |
ax.add_collection(coll) |
| 123 |
|
| 124 |
plt.plot(x, y, marker='o', color='black', linestyle='none') |
| 125 |
plt.plot(x[shared_vertices], y[shared_vertices], marker='o', color='green', linestyle='none') |
| 126 |
plt.plot(x[ghost_vertices], y[ghost_vertices], marker='o', color='yellow', linestyle='none') |
| 127 |
|
| 128 |
xlim = ax.get_xlim() |
| 129 |
ylim = ax.get_ylim() |
| 130 |
|
| 131 |
plt.xlim((xlim[0] - 0.1, xlim[1] + 0.1)) |
| 132 |
plt.ylim((ylim[0] - 0.1, ylim[1] + 0.1)) |
| 133 |
|
| 134 |
for note in cells_note: |
| 135 |
plt.text(note[0], note[1], note[2], verticalalignment='center', |
| 136 |
horizontalalignment='center', size=8) |
| 137 |
|
| 138 |
for note in verts_note: |
| 139 |
plt.text(note[0], note[1], note[2], size=8, verticalalignment='center') |
| 140 |
|
| 141 |
for note in facet_note: |
| 142 |
plt.text(note[0], note[1], note[2], size=8, verticalalignment='center', backgroundcolor=note[3]) |
| 143 |
|
| 144 |
# Q = MeshFunction("double", mesh, mesh.topology().dim()-1) |
| 145 |
|
| 146 |
# # Save solution in XDMF format if available |
| 147 |
# xdmf = XDMFFile(mesh.mpi_comm(), "Q.xdmf") |
| 148 |
# if has_hdf5(): |
| 149 |
# xdmf.write(Q) |
| 150 |
# elif MPI.size(mesh.mpi_comm()) == 1: |
| 151 |
# encoding = XDMFFile.Encoding.ASCII |
| 152 |
# xdmf.write(Q, encoding) |
| 153 |
# else: |
| 154 |
# # Save solution in vtk format |
| 155 |
# xdmf = File("Q.pvd") |
| 156 |
# xdmf << Q |
| 157 |
|
| 158 |
plt.savefig("mesh-rank%d.png" % rank) |
| 159 |
if os.environ.get("DOLFIN_NOPLOT", "0") == "0": |
| 160 |
plt.show() |