1
// Copyright (C) 2005-2006 Anders Logg.
2
// Licensed under the GNU LGPL Version 2.1.
4
// Modified by Garth N. Wells 2007.
6
// First added: 2005-12-02
7
// Last changed: 2007-1-06
9
#include <dolfin/MeshEditor.h>
10
#include <dolfin/UnitSquare.h>
11
#include <dolfin/MPI.h>
12
#include <dolfin/MPIMeshCommunicator.h>
14
using namespace dolfin;
16
//-----------------------------------------------------------------------------
17
UnitSquare::UnitSquare(uint nx, uint ny, Type type) : Mesh()
19
// Receive mesh according to parallel policy
20
if (MPI::receive()) { MPIMeshCommunicator::receive(*this); return; }
22
if ( nx < 1 || ny < 1 )
23
error("Size of unit square must be at least 1 in each dimension.");
25
rename("mesh", "Mesh of the unit square (0,1) x (0,1)");
27
// Open mesh for editing
29
editor.open(*this, CellType::triangle, 2, 2);
31
// Create vertices and cells:
32
if (type == crisscross)
34
editor.initVertices((nx+1)*(ny+1) + nx*ny);
35
editor.initCells(4*nx*ny);
39
editor.initVertices((nx+1)*(ny+1));
40
editor.initCells(2*nx*ny);
43
// Create main vertices:
45
for (uint iy = 0; iy <= ny; iy++)
47
const real y = static_cast<real>(iy) / static_cast<real>(ny);
48
for (uint ix = 0; ix <= nx; ix++)
50
const real x = static_cast<real>(ix) / static_cast<real>(nx);
51
editor.addVertex(vertex++, x, y);
55
// Create midpoint vertices if the mesh type is crisscross
56
if (type == crisscross)
58
for (uint iy = 0; iy < ny; iy++)
60
const real y = (static_cast<real>(iy) + 0.5) / static_cast<real>(ny);
61
for (uint ix = 0; ix < nx; ix++)
63
const real x = (static_cast<real>(ix) + 0.5) / static_cast<real>(nx);
64
editor.addVertex(vertex++, x, y);
71
if (type == crisscross)
73
for (uint iy = 0; iy < ny; iy++)
75
for (uint ix = 0; ix < nx; ix++)
77
const uint v0 = iy*(nx + 1) + ix;
78
const uint v1 = v0 + 1;
79
const uint v2 = v0 + (nx + 1);
80
const uint v3 = v1 + (nx + 1);
81
const uint vmid = (nx + 1)*(ny + 1) + iy*nx + ix;
83
// Note that v0 < v1 < v2 < v3 < vmid.
84
editor.addCell(cell++, v0, v1, vmid);
85
editor.addCell(cell++, v0, v2, vmid);
86
editor.addCell(cell++, v1, v3, vmid);
87
editor.addCell(cell++, v2, v3, vmid);
91
else if (type == left )
93
for (uint iy = 0; iy < ny; iy++)
95
for (uint ix = 0; ix < nx; ix++)
97
const uint v0 = iy*(nx + 1) + ix;
98
const uint v1 = v0 + 1;
99
const uint v2 = v0 + (nx + 1);
100
const uint v3 = v1 + (nx + 1);
102
editor.addCell(cell++, v0, v1, v2);
103
editor.addCell(cell++, v1, v2, v3);
109
for (uint iy = 0; iy < ny; iy++)
111
for (uint ix = 0; ix < nx; ix++)
113
const uint v0 = iy*(nx + 1) + ix;
114
const uint v1 = v0 + 1;
115
const uint v2 = v0 + (nx + 1);
116
const uint v3 = v1 + (nx + 1);
118
editor.addCell(cell++, v0, v1, v3);
119
editor.addCell(cell++, v0, v2, v3);
127
// Broadcast mesh according to parallel policy
128
if (MPI::broadcast()) { MPIMeshCommunicator::broadcast(*this); }
130
//-----------------------------------------------------------------------------