Triangle.cpp 9.75 KB
Newer Older
Michael Turner's avatar
Michael Turner committed
1
2
////////////////////////////////////////////////////////////////////////////////
//
Michael Turner's avatar
Michael Turner committed
3
//  File: Triangle.cpp
Michael Turner's avatar
Michael Turner committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//  Department of Aeronautics, Imperial College London (UK), and Scientific
//  Computing and Imaging Institute, University of Utah (USA).
//
//  License for the specific language governing rights and limitations under
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included
//  in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
Michael Turner's avatar
Michael Turner committed
32
//  Description: Mesh triangle object.
Michael Turner's avatar
Michael Turner committed
33
34
35
36
37
//
////////////////////////////////////////////////////////////////////////////////

#include <StdRegions/StdNodalTriExp.h>
#include <LocalRegions/TriExp.h>
38
#include <NekMeshUtils/MeshElements/Triangle.h>
Michael Turner's avatar
Michael Turner committed
39

Michael Turner's avatar
Michael Turner committed
40
41
#include <LibUtilities/Foundations/ManagerAccess.h>

Michael Turner's avatar
Michael Turner committed
42
43
44
45
using namespace std;

namespace Nektar
{
46
namespace NekMeshUtils
Michael Turner's avatar
Michael Turner committed
47
48
{

49
50
51
LibUtilities::ShapeType Triangle::m_type =
    GetElementFactory().RegisterCreatorFunction(
        LibUtilities::eTriangle, Triangle::create, "Triangle");
Michael Turner's avatar
Michael Turner committed
52
53
54
55

/**
 * @brief Create a triangle element.
 */
56
Triangle::Triangle(ElmtConfig pConf,
Michael Turner's avatar
Michael Turner committed
57
                   vector<NodeSharedPtr> pNodeList,
58
                   vector<int> pTagList)
Michael Turner's avatar
Michael Turner committed
59
60
    : Element(pConf, GetNumNodes(pConf), pNodeList.size())
{
61
62
63
    m_tag       = "T";
    m_dim       = 2;
    m_taglist   = pTagList;
Michael Turner's avatar
Michael Turner committed
64
    m_curveType = LibUtilities::eNodalTriEvenlySpaced;
65
    int n       = m_conf.m_order - 1;
Michael Turner's avatar
Michael Turner committed
66
67
68
69

    // Create a map to relate edge nodes to a pair of vertices
    // defining an edge. This is based on the ordering produced by
    // gmsh.
70
71
72
73
74
    map<pair<int, int>, int> edgeNodeMap;
    map<pair<int, int>, int>::iterator it;
    edgeNodeMap[pair<int, int>(1, 2)] = 4;
    edgeNodeMap[pair<int, int>(2, 3)] = 4 + n;
    edgeNodeMap[pair<int, int>(3, 1)] = 4 + 2 * n;
Michael Turner's avatar
Michael Turner committed
75
76
77
78

    // Add vertices. This logic will determine (in 2D) whether the
    // element is clockwise (sum > 0) or counter-clockwise (sum < 0).
    NekDouble sum = 0.0;
79
80
81
    for (int i = 0; i < 3; ++i)
    {
        int o = (i + 1) % 3;
Michael Turner's avatar
Michael Turner committed
82
83
84
85
86
87
88
89
90
        m_vertex.push_back(pNodeList[i]);
        sum += (pNodeList[o]->m_x - pNodeList[i]->m_x) *
               (pNodeList[o]->m_y + pNodeList[i]->m_y);
    }

    // Create edges (with corresponding set of edge points)
    for (it = edgeNodeMap.begin(); it != edgeNodeMap.end(); ++it)
    {
        vector<NodeSharedPtr> edgeNodes;
91
92
93
94
95
        if (m_conf.m_order > 1)
        {
            for (int j = it->second; j < it->second + n; ++j)
            {
                edgeNodes.push_back(pNodeList[j - 1]);
Michael Turner's avatar
Michael Turner committed
96
97
            }
        }
98
99
        m_edge.push_back(EdgeSharedPtr(new Edge(pNodeList[it->first.first - 1],
                                                pNodeList[it->first.second - 1],
Michael Turner's avatar
Michael Turner committed
100
101
102
103
104
105
106
107
                                                edgeNodes,
                                                m_conf.m_edgeCurveType)));
    }

    if (pConf.m_reorient)
    {
        if (sum > 0.0)
        {
108
            swap(m_vertex[1], m_vertex[2]);
Michael Turner's avatar
Michael Turner committed
109
110
111
112
113
114
115
            reverse(m_edge.begin(), m_edge.end());
        }
    }

    if (m_conf.m_faceNodes)
    {
        m_volumeNodes.insert(m_volumeNodes.begin(),
116
117
                             pNodeList.begin() + 3 * m_conf.m_order,
                             pNodeList.end());
Michael Turner's avatar
Michael Turner committed
118
119
120
121
122
    }
}

SpatialDomains::GeometrySharedPtr Triangle::GetGeom(int coordDim)
{
123
    SpatialDomains::SegGeomSharedPtr edges[3];
Michael Turner's avatar
Michael Turner committed
124
    SpatialDomains::PointGeomSharedPtr verts[3];
125
    SpatialDomains::TriGeomSharedPtr ret;
Michael Turner's avatar
Michael Turner committed
126
127
128

    for (int i = 0; i < 3; ++i)
    {
129
        edges[i] = m_edge[i]->GetGeom(coordDim);
Michael Turner's avatar
Michael Turner committed
130
131
132
133
134
135
        verts[i] = m_vertex[i]->GetGeom(coordDim);
    }

    StdRegions::Orientation edgeorient[3] = {
        SpatialDomains::SegGeom::GetEdgeOrientation(*edges[0], *edges[1]),
        SpatialDomains::SegGeom::GetEdgeOrientation(*edges[1], *edges[2]),
136
        SpatialDomains::SegGeom::GetEdgeOrientation(*edges[2], *edges[0])};
Michael Turner's avatar
Michael Turner committed
137

138
139
    ret = MemoryManager<SpatialDomains::TriGeom>::AllocateSharedPtr(
        m_id, verts, edges, edgeorient);
Michael Turner's avatar
Michael Turner committed
140
141
142
143

    return ret;
}

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
StdRegions::Orientation Triangle::GetEdgeOrient(int edgeId, EdgeSharedPtr edge)
{
    int locVert = edgeId;
    if (edge->m_n1 == m_vertex[locVert])
    {
        return StdRegions::eForwards;
    }
    else if (edge->m_n2 == m_vertex[locVert])
    {
        return StdRegions::eBackwards;
    }
    else
    {
        ASSERTL1(false, "Edge is not connected to this triangle.");
    }

    return StdRegions::eNoOrientation;
}

Michael Turner's avatar
Michael Turner committed
163
164
165
166
167
168
169
/**
 * @brief Return the number of nodes defining a triangle.
 */
unsigned int Triangle::GetNumNodes(ElmtConfig pConf)
{
    int n = pConf.m_order;
    if (!pConf.m_faceNodes)
170
        return (n + 1) + 2 * (n - 1) + 1;
Michael Turner's avatar
Michael Turner committed
171
    else
172
        return (n + 1) * (n + 2) / 2;
Michael Turner's avatar
Michael Turner committed
173
174
}

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
void Triangle::GetCurvedNodes(std::vector<NodeSharedPtr> &nodeList) const
{
    int n = m_edge[0]->GetNodeCount();
    nodeList.resize(n * (n + 1) / 2);

    // Populate nodelist
    std::copy(m_vertex.begin(), m_vertex.end(), nodeList.begin());
    for (int i = 0; i < 3; ++i)
    {
        std::copy(m_edge[i]->m_edgeNodes.begin(),
                  m_edge[i]->m_edgeNodes.end(),
                  nodeList.begin() + 3 + i * (n - 2));
        if (m_edge[i]->m_n1 != m_vertex[i])
        {
            // If edge orientation is reversed relative to node ordering, we
            // need to reverse order of nodes.
            std::reverse(nodeList.begin() + 3 + i * (n - 2),
                         nodeList.begin() + 3 + (i + 1) * (n - 2));
        }
    }

    // Copy volume nodes.
    std::copy(m_volumeNodes.begin(),
              m_volumeNodes.end(),
              nodeList.begin() + 3 * (n - 1));
}

202
203
204
205
void Triangle::MakeOrder(int                                order,
                         SpatialDomains::GeometrySharedPtr  geom,
                         LibUtilities::PointsType           pType,
                         int                                coordDim,
206
207
                         int                               &id,
                         bool                               justConfig)
208

Michael Turner's avatar
Michael Turner committed
209
{
210
211
212
213
214
    m_conf.m_order       = order;
    m_curveType          = pType;
    m_conf.m_volumeNodes = false;
    m_volumeNodes.clear();

215
    // Triangles of order < 3 have no interior volume points.
216
217
218
219
220
221
222
223
224
    if (order == 1 || order == 2)
    {
        m_conf.m_faceNodes = false;
        return;
    }

    m_conf.m_faceNodes = true;

    if (justConfig)
Michael Turner's avatar
Michael Turner committed
225
    {
226
        return;
Michael Turner's avatar
Michael Turner committed
227
228
    }

229
230
231
232
233
234
235
236
237
238
239
    int nPoints = order + 1;
    StdRegions::StdExpansionSharedPtr xmap = geom->GetXmap();

    Array<OneD, NekDouble> px, py;
    LibUtilities::PointsKey pKey(nPoints, pType);
    ASSERTL1(pKey.GetPointsDim() == 2, "Points distribution must be 2D");
    LibUtilities::PointsManager()[pKey]->GetPoints(px, py);

    Array<OneD, Array<OneD, NekDouble> > phys(coordDim);

    for (int i = 0; i < coordDim; ++i)
Michael Turner's avatar
Michael Turner committed
240
    {
241
242
        phys[i] = Array<OneD, NekDouble>(xmap->GetTotPoints());
        xmap->BwdTrans(geom->GetCoeffs(i), phys[i]);
Michael Turner's avatar
Michael Turner committed
243
244
    }

245
246
247
248
249
    const int nTriPts = nPoints * (nPoints + 1) / 2;
    const int nTriIntPts = (nPoints - 3) * (nPoints - 2) / 2;
    m_volumeNodes.resize(nTriIntPts);

    for (int i = 3 + 3*(nPoints-2), cnt = 0; i < nTriPts; ++i, ++cnt)
Michael Turner's avatar
Michael Turner committed
250
    {
251
252
253
254
255
256
257
258
259
260
261
262
        Array<OneD, NekDouble> xp(2);
        xp[0] = px[i];
        xp[1] = py[i];

        Array<OneD, NekDouble> x(3, 0.0);
        for (int j = 0; j < coordDim; ++j)
        {
            x[j] = xmap->PhysEvaluate(xp, phys[j]);
        }

        m_volumeNodes[cnt] = boost::shared_ptr<Node>(
            new Node(id++, x[0], x[1], x[2]));
Michael Turner's avatar
Michael Turner committed
263
264
265
266
    }

    m_conf.m_order       = order;
    m_conf.m_faceNodes   = true;
267
    m_conf.m_volumeNodes = false;
Michael Turner's avatar
Michael Turner committed
268
}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

Array<OneD, NekDouble> Triangle::Normal(bool inward)
{
    Array<OneD, NekDouble> ret(3,0.0);

    ret[0] = (m_vertex[1]->m_y - m_vertex[0]->m_y) * (m_vertex[2]->m_z - m_vertex[0]->m_z) -
             (m_vertex[1]->m_z - m_vertex[0]->m_z) * (m_vertex[2]->m_y - m_vertex[0]->m_y);
    ret[1] = (m_vertex[1]->m_z - m_vertex[0]->m_z) * (m_vertex[2]->m_x - m_vertex[0]->m_x) -
             (m_vertex[1]->m_x - m_vertex[0]->m_x) * (m_vertex[2]->m_z - m_vertex[0]->m_z);
    ret[2] = (m_vertex[1]->m_x - m_vertex[0]->m_x) * (m_vertex[2]->m_y - m_vertex[0]->m_y) -
             (m_vertex[1]->m_y - m_vertex[0]->m_y) * (m_vertex[2]->m_x - m_vertex[0]->m_x);

    NekDouble mt = ret[0] * ret[0] + ret[1] * ret[1] + ret[2] * ret[2];
    mt           = sqrt(mt);

    ret[0] /= mt;
    ret[1] /= mt;
    ret[2] /= mt;

    if(m_parentCAD)
    {
290
        //has cad so can orientate based on that
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
        if(m_parentCAD->Orientation() == CADOrientation::eBackwards)
        {
            ret[0] *= -1.0;
            ret[1] *= -1.0;
            ret[2] *= -1.0;
        }

        //by default normals point outwards so if we want inward for BLs
        if(inward)
        {
            ret[0] *= -1.0;
            ret[1] *= -1.0;
            ret[2] *= -1.0;
        }
    }
    return ret;
}

Michael Turner's avatar
Michael Turner committed
309
310
}
}