MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
geometryelement.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#include "geometryelement.h"
8
9#include "UTIL/debug.h"
10#include "UTIL/functions.h"
11#include "UTIL/maiamath.h"
12#include "UTIL/timer.h"
13
14using namespace std;
15
16//----------------------------------------------------------
17// Definitions of class element
18//----------------------------------------------------------
19
20template <MInt nDim>
22
23template <MInt nDim>
24void GeometryElement<nDim>::allocateElements(void* tmpPointer, void* /*dummy*/, MInt& /*dummy2*/) {
25 // TRACE();
26
27 m_vertices = (MFloat**)tmpPointer;
28 // Jump memory for pointers to pointers
29 for(MInt i = 0; i < nDim; i++) {
30 tmpPointer = (void*)((MFloat**)tmpPointer + 1);
31 }
32
33 for(MInt i = 0; i < nDim; i++) {
34 // Set pointers to pointers
35 m_vertices[i] = (MFloat*)tmpPointer;
36 // Jump memory for values
37 tmpPointer = (void*)((MFloat*)tmpPointer + nDim);
38 }
39 m_normal = (MFloat*)tmpPointer;
40 tmpPointer = (void*)((MFloat*)tmpPointer + nDim);
41 m_minMax = (MFloat*)tmpPointer;
42}
43
44template <MInt nDim>
46 // TRACE();
47
48 for(MInt j = 0; j < nDim; j++) {
49 m_minMax[j] = m_vertices[0][j];
50 m_minMax[j + nDim] = m_vertices[0][j];
51 }
52 for(MInt i = 0; i < nDim; i++) {
53 for(MInt j = 0; j < nDim; j++) {
54 // Find maximum
55 m_minMax[j + nDim] = (m_minMax[j + nDim] < m_vertices[i][j]) ? m_vertices[i][j] : m_minMax[j + nDim];
56 // Find minimum
57 m_minMax[j] = (m_minMax[j] > m_vertices[i][j]) ? m_vertices[i][j] : m_minMax[j];
58 }
59 }
60}
61
62template <MInt nDim>
64 TRACE();
65
66 cerr.precision(10);
67 cerr << "------------------------------" << endl;
68 // M_Normal vector only in 3D (for now)
69 IF_CONSTEXPR(nDim == 3) { cerr << "M_Normal : " << m_normal[0] << " " << m_normal[1] << " " << m_normal[2] << endl; }
70
71 for(MInt i = 0; i < nDim; i++) {
72 cerr << "Vertex " << i << ": ";
73 for(MInt j = 0; j < nDim; j++) {
74 cerr << m_vertices[i][j] << " ";
75 }
76 cerr << endl;
77 }
78 cerr << "Bounding box: " << endl << "m_min: ";
79
80 for(MInt i = 0; i < nDim; i++) {
81 cerr << m_minMax[i] << " ";
82 }
83 cerr << endl;
84 cerr << "Bounding box: " << endl << "m_max: ";
85
86 for(MInt i = 0; i < nDim; i++) {
87 cerr << m_minMax[i + nDim] << " ";
88 }
89 cerr << endl;
90}
91
92
102template <MInt nDim>
103void GeometryElement<nDim>::calcNormal(const MFloat* const vertices, MFloat* normal) const {
104 TRACE();
105
106 using Vec2D = array<MFloat, 2>;
107 using Vec3D = array<MFloat, 3>;
108
109 // Since the used algorithms depend on the dimension, we need to branch here
110 IF_CONSTEXPR(nDim == 2) {
111 Vec2D edge, norm;
112
113 // Calculate edge vector
114 edge[0] = vertices[2] - vertices[0];
115 edge[1] = vertices[3] - vertices[1];
116
117 // Calculate vector normal to edge
118 norm[0] = edge[1];
119 norm[1] = -edge[0];
120
121 // Normalize normal vector and copy to result
123 copy(norm.begin(), norm.end(), normal);
124 }
125 else IF_CONSTEXPR(nDim == 3) {
126 Vec3D edge0, edge1, norm;
127
128 // Calculate edge vectors
129 edge0[0] = vertices[3] - vertices[0];
130 edge0[1] = vertices[4] - vertices[1];
131 edge0[2] = vertices[5] - vertices[2];
132 edge1[0] = vertices[6] - vertices[0];
133 edge1[1] = vertices[7] - vertices[1];
134 edge1[2] = vertices[8] - vertices[2];
135
136 // Calculate vector normal to plane
137 norm = maia::math::cross(edge0, edge1);
138
139 // Normalize normal vector and copy to result
141 copy(norm.begin(), norm.end(), normal);
142 }
143 else {
144 TERMM(1, "Bad number of dimensions.");
145 }
146}
147
148
158template <MInt nDim>
159void GeometryElement<nDim>::calcCentroid(const MFloat* const vertices, MFloat* centroid) const {
160 TRACE();
161
162 // Calculate sums over vertices and divide by dimensions (works for 2D and 3D)
163 // Example 2D:
164 // vertices: (x0, y0), (x1, y1)
165 // centroid: ((x0 + x1)/2, (y0 + y1)/2)
166 for(MInt dim = 0; dim < nDim; dim++) {
167 centroid[dim] = 0.0;
168 for(MInt vertexId = 0; vertexId < nDim; vertexId++) {
169 centroid[dim] += vertices[(vertexId * nDim) + dim];
170 }
171 centroid[dim] /= static_cast<MFloat>(nDim);
172 }
173}
174
175
183template <MInt nDim>
185 TRACE();
186
187 IF_CONSTEXPR(nDim == 2) {
188 // Copy vertices information to vectors
189 vertices[0] = m_vertices[0][0];
190 vertices[1] = m_vertices[0][1];
191 vertices[2] = m_vertices[1][0];
192 vertices[3] = m_vertices[1][1];
193 }
194 else IF_CONSTEXPR(nDim == 3) {
195 // Copy vertices information to vectors
196 vertices[0] = m_vertices[0][0];
197 vertices[1] = m_vertices[0][1];
198 vertices[2] = m_vertices[0][2];
199 vertices[3] = m_vertices[1][0];
200 vertices[4] = m_vertices[1][1];
201 vertices[5] = m_vertices[1][2];
202 vertices[6] = m_vertices[2][0];
203 vertices[7] = m_vertices[2][1];
204 vertices[8] = m_vertices[2][2];
205 }
206}
207
208
209// Explicit instantiations for 2D and 3D
210template class GeometryElement<2>;
211template class GeometryElement<3>;
void calcNormal(const MFloat *const vertices, MFloat *normal) const
Calculates the normal vector from the geometry element vertices.
void allocateElements(void *, void *, MInt &)
void writeElement() const
void calcCentroid(const MFloat *const vertices, MFloat *centroid) const
Calculate the centroid of the geometry element vertices.
void getVertices(MFloat *vertices) const
Return the vertices of the geometry element.
int32_t MInt
Definition: maiatypes.h:62
double MFloat
Definition: maiatypes.h:52
void cross(const T *const u, const T *const v, T *const c)
Definition: maiamath.h:101
void normalize(std::array< T, N > &u)
Definition: maiamath.h:191