~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/kernel/mesh/UnitSquare.cpp

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2005-2006 Anders Logg.
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// Modified by Garth N. Wells 2007.
5
 
//
6
 
// First added:  2005-12-02
7
 
// Last changed: 2007-1-06
8
 
 
9
 
#include <dolfin/MeshEditor.h>
10
 
#include <dolfin/UnitSquare.h>
11
 
#include <dolfin/MPI.h>
12
 
#include <dolfin/MPIMeshCommunicator.h>
13
 
 
14
 
using namespace dolfin;
15
 
 
16
 
//-----------------------------------------------------------------------------
17
 
UnitSquare::UnitSquare(uint nx, uint ny, Type type) : Mesh()
18
 
{
19
 
  // Receive mesh according to parallel policy
20
 
  if (MPI::receive()) { MPIMeshCommunicator::receive(*this); return; }
21
 
  
22
 
  if ( nx < 1 || ny < 1 )
23
 
    error("Size of unit square must be at least 1 in each dimension.");
24
 
 
25
 
  rename("mesh", "Mesh of the unit square (0,1) x (0,1)");
26
 
 
27
 
  // Open mesh for editing
28
 
  MeshEditor editor;
29
 
  editor.open(*this, CellType::triangle, 2, 2);
30
 
 
31
 
  // Create vertices and cells:
32
 
  if (type == crisscross) 
33
 
  {
34
 
    editor.initVertices((nx+1)*(ny+1) + nx*ny);
35
 
    editor.initCells(4*nx*ny);
36
 
  } 
37
 
  else 
38
 
  {
39
 
    editor.initVertices((nx+1)*(ny+1));
40
 
    editor.initCells(2*nx*ny);
41
 
  }
42
 
  
43
 
  // Create main vertices:
44
 
  uint vertex = 0;
45
 
  for (uint iy = 0; iy <= ny; iy++) 
46
 
  {
47
 
    const real y = static_cast<real>(iy) / static_cast<real>(ny);
48
 
    for (uint ix = 0; ix <= nx; ix++) 
49
 
    {
50
 
      const real x = static_cast<real>(ix) / static_cast<real>(nx);
51
 
      editor.addVertex(vertex++, x, y);
52
 
    }
53
 
  }
54
 
  
55
 
  // Create midpoint vertices if the mesh type is crisscross
56
 
  if (type == crisscross) 
57
 
  {
58
 
    for (uint iy = 0; iy < ny; iy++) 
59
 
    {
60
 
      const real y = (static_cast<real>(iy) + 0.5) / static_cast<real>(ny);
61
 
      for (uint ix = 0; ix < nx; ix++) 
62
 
      {
63
 
        const real x = (static_cast<real>(ix) + 0.5) / static_cast<real>(nx);
64
 
        editor.addVertex(vertex++, x, y);
65
 
      }
66
 
    }
67
 
  }
68
 
 
69
 
  // Create triangles
70
 
  uint cell = 0;
71
 
  if (type == crisscross) 
72
 
  {
73
 
    for (uint iy = 0; iy < ny; iy++) 
74
 
    {
75
 
      for (uint ix = 0; ix < nx; ix++) 
76
 
      {
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;
82
 
        
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);
88
 
      }
89
 
    }
90
 
  } 
91
 
  else if (type == left ) 
92
 
  {
93
 
    for (uint iy = 0; iy < ny; iy++) 
94
 
    {
95
 
      for (uint ix = 0; ix < nx; ix++) 
96
 
      {
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);
101
 
 
102
 
        editor.addCell(cell++, v0, v1, v2);
103
 
        editor.addCell(cell++, v1, v2, v3);
104
 
      }
105
 
    }
106
 
  } 
107
 
  else 
108
 
  { 
109
 
    for (uint iy = 0; iy < ny; iy++) 
110
 
    {
111
 
      for (uint ix = 0; ix < nx; ix++) 
112
 
      {
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);
117
 
 
118
 
        editor.addCell(cell++, v0, v1, v3);
119
 
        editor.addCell(cell++, v0, v2, v3);
120
 
      }
121
 
    }
122
 
  }
123
 
 
124
 
  // Close mesh editor
125
 
  editor.close();
126
 
 
127
 
  // Broadcast mesh according to parallel policy
128
 
  if (MPI::broadcast()) { MPIMeshCommunicator::broadcast(*this); }
129
 
}
130
 
//-----------------------------------------------------------------------------