VMTK
Loading...
Searching...
No Matches
vtkvmtkFEShapeFunctions.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkFEShapeFunctions.cxx,v $
5 Language: C++
6 Date: $Date: 2005/11/15 17:39:25 $
7 Version: $Revision: 1.3 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23#include "vtkObjectFactory.h"
24#include "vtkCellType.h"
25#include "vtkLine.h"
26#include "vtkQuadraticEdge.h"
27#include "vtkQuad.h"
28#include "vtkQuadraticQuad.h"
29#include "vtkBiQuadraticQuad.h"
30#include "vtkTriangle.h"
31#include "vtkQuadraticTriangle.h"
32#include "vtkHexahedron.h"
33#include "vtkQuadraticHexahedron.h"
34#include "vtkTriQuadraticHexahedron.h"
35#include "vtkWedge.h"
36#include "vtkQuadraticWedge.h"
37#include "vtkVersion.h"
38#include "vtkBiQuadraticQuadraticWedge.h"
39#include "vtkTetra.h"
40#include "vtkQuadraticTetra.h"
41#include "vtkPoints.h"
42#include "vtkMath.h"
43
44//#define VTKVMTKFESHAPEFUNCTIONS_NEGATIVE_JACOBIAN_WARNING
45
47
48
50{
51 this->Phi = vtkDoubleArray::New();
52 this->DPhi = vtkDoubleArray::New();
53 this->Jacobians = vtkDoubleArray::New();
54 this->NumberOfCellPoints = -1;
55}
56
58{
59 this->Phi->Delete();
60 this->DPhi->Delete();
61 this->Jacobians->Delete();
62}
63
64void vtkvmtkFEShapeFunctions::Initialize(vtkCell* cell, vtkDoubleArray* pcoords)
65{
66 vtkIdType cellDimension = cell->GetCellDimension();
67 vtkIdType numberOfPCoords = pcoords->GetNumberOfTuples();
68 vtkIdType numberOfCellPoints = cell->GetNumberOfPoints();
69 this->NumberOfCellPoints = numberOfCellPoints;
70
71 this->Phi->Initialize();
72 this->Phi->SetNumberOfTuples(numberOfCellPoints*numberOfPCoords);
73
74 this->DPhi->Initialize();
75 this->DPhi->SetNumberOfComponents(3);
76 this->DPhi->SetNumberOfTuples(numberOfCellPoints*numberOfPCoords);
77
78 this->Jacobians->Initialize();
79 this->Jacobians->SetNumberOfTuples(numberOfPCoords);
80
81 int i;
82
83 //Phi
84 for (i=0; i<numberOfPCoords; i++)
85 {
86 double* sf = new double[numberOfCellPoints];
87 this->GetInterpolationFunctions(cell,pcoords->GetTuple(i),sf);
88 int j;
89 for (j=0; j<numberOfCellPoints; j++)
90 {
91 this->Phi->SetValue(i*numberOfCellPoints+j,sf[j]);
92 }
93 delete[] sf;
94
95 //DPhi
96 double* derivs = new double[cellDimension*numberOfCellPoints];
97 this->GetInterpolationDerivs(cell,pcoords->GetTuple(i),derivs);
98 if (cellDimension == 2)
99 {
100 double inverseJacobianMatrix[2][3];
101 this->ComputeInverseJacobianMatrix2D(cell,pcoords->GetTuple(i),inverseJacobianMatrix);
102 for (j=0; j<numberOfCellPoints; j++)
103 {
104 int k;
105 for (k=0; k<3; k++)
106 {
107 double dphik = derivs[j] * inverseJacobianMatrix[0][k] + derivs[j+numberOfCellPoints] * inverseJacobianMatrix[1][k];
108 this->DPhi->SetComponent(i*numberOfCellPoints+j,k,dphik);
109 }
110 }
111 }
112 else if (cellDimension == 3)
113 {
114 double inverseJacobianMatrix[3][3];
115 this->ComputeInverseJacobianMatrix3D(cell,pcoords->GetTuple(i),inverseJacobianMatrix);
116 for (j=0; j<numberOfCellPoints; j++)
117 {
118 int k;
119 for (k=0; k<3; k++)
120 {
121 double dphik = derivs[j] * inverseJacobianMatrix[0][k] + derivs[j+numberOfCellPoints] * inverseJacobianMatrix[1][k] + derivs[j+2*numberOfCellPoints] * inverseJacobianMatrix[2][k];
122 this->DPhi->SetComponent(i*numberOfCellPoints+j,k,dphik);
123 }
124 }
125 }
126 delete[] derivs;
127
128 //Jacobians
129 double jacobian = this->ComputeJacobian(cell,pcoords->GetTuple(i));
130 this->Jacobians->SetValue(i,jacobian);
131 }
132}
133
134void vtkvmtkFEShapeFunctions::ComputeInverseJacobianMatrix2D(vtkCell* cell, double* pcoords, double inverseJacobianMatrix[2][3])
135{
136 int cellDimension = cell->GetCellDimension();
137
138 if (cellDimension != 2)
139 {
140 vtkGenericWarningMacro("Error: ComputeInverseJacobian2D only works for 2D cells.");
141 return;
142 }
143
144 int numberOfCellPoints = cell->GetNumberOfPoints();
145 double* derivs = new double[2*numberOfCellPoints];
146
148
149 int i, j;
150
151 double jacobianMatrixTr[2][3];
152 for (i=0; i<3; i++)
153 {
154 jacobianMatrixTr[0][i] = jacobianMatrixTr[1][i] = 0.0;
155 }
156
157 double x[3];
158 for (j=0; j<numberOfCellPoints; j++)
159 {
160 cell->GetPoints()->GetPoint(j,x);
161 for (i=0; i<3; i++)
162 {
163 jacobianMatrixTr[0][i] += x[i] * derivs[j];
164 jacobianMatrixTr[1][i] += x[i] * derivs[numberOfCellPoints+j];
165 }
166 }
167 delete[] derivs;
168
169 double jacobianMatrixSquared[2][2];
170 jacobianMatrixSquared[0][0] = vtkMath::Dot(jacobianMatrixTr[0],jacobianMatrixTr[0]);
171 jacobianMatrixSquared[0][1] = vtkMath::Dot(jacobianMatrixTr[0],jacobianMatrixTr[1]);
172 jacobianMatrixSquared[1][0] = vtkMath::Dot(jacobianMatrixTr[1],jacobianMatrixTr[0]);
173 jacobianMatrixSquared[1][1] = vtkMath::Dot(jacobianMatrixTr[1],jacobianMatrixTr[1]);
174
175 double jacobianSquared = vtkMath::Determinant2x2(jacobianMatrixSquared[0],jacobianMatrixSquared[1]);
176
177 if (jacobianSquared < 0.0)
178 {
179#ifdef VTKVMTKFESHAPEFUNCTIONS_NEGATIVE_JACOBIAN_WARNING
180 vtkGenericWarningMacro("Warning: negative determinant of squared Jacobian, taking absolute value.");
181#endif
182 jacobianSquared = fabs(jacobianSquared);
183 }
184
185 double inverseJacobianSquared = 1.0 / jacobianSquared;
186
187 double inverseJacobianMatrixSquared[2][2];
188 inverseJacobianMatrixSquared[0][0] = jacobianMatrixSquared[1][1] * inverseJacobianSquared;
189 inverseJacobianMatrixSquared[0][1] = -jacobianMatrixSquared[0][1] * inverseJacobianSquared;
190 inverseJacobianMatrixSquared[1][0] = -jacobianMatrixSquared[1][0] * inverseJacobianSquared;
191 inverseJacobianMatrixSquared[1][1] = jacobianMatrixSquared[0][0] * inverseJacobianSquared;
192
193 inverseJacobianMatrix[0][0] = inverseJacobianMatrixSquared[0][0] * jacobianMatrixTr[0][0] + inverseJacobianMatrixSquared[0][1] * jacobianMatrixTr[1][0];
194 inverseJacobianMatrix[0][1] = inverseJacobianMatrixSquared[0][0] * jacobianMatrixTr[0][1] + inverseJacobianMatrixSquared[0][1] * jacobianMatrixTr[1][1];
195 inverseJacobianMatrix[0][2] = inverseJacobianMatrixSquared[0][0] * jacobianMatrixTr[0][2] + inverseJacobianMatrixSquared[0][1] * jacobianMatrixTr[1][2];
196 inverseJacobianMatrix[1][0] = inverseJacobianMatrixSquared[1][0] * jacobianMatrixTr[0][0] + inverseJacobianMatrixSquared[1][1] * jacobianMatrixTr[1][0];
197 inverseJacobianMatrix[1][1] = inverseJacobianMatrixSquared[1][0] * jacobianMatrixTr[0][1] + inverseJacobianMatrixSquared[1][1] * jacobianMatrixTr[1][1];
198 inverseJacobianMatrix[1][2] = inverseJacobianMatrixSquared[1][0] * jacobianMatrixTr[0][2] + inverseJacobianMatrixSquared[1][1] * jacobianMatrixTr[1][2];
199}
200
201void vtkvmtkFEShapeFunctions::ComputeInverseJacobianMatrix3D(vtkCell* cell, double* pcoords, double inverseJacobianMatrix[3][3])
202{
203 int cellDimension = cell->GetCellDimension();
204
205 if (cellDimension != 3)
206 {
207 vtkGenericWarningMacro("Error: ComputeInverseJacobian3D only works for 3D cells.");
208 return;
209 }
210
211 int numberOfCellPoints = cell->GetNumberOfPoints();
212 double* derivs = new double[3*numberOfCellPoints];
213
215
216 int i, j;
217
218 double jacobianMatrix[3][3];
219 for (i=0; i<3; i++)
220 {
221 jacobianMatrix[0][i] = jacobianMatrix[1][i] = jacobianMatrix[2][i] = 0.0;
222 }
223
224 double x[3];
225 for (j=0; j<numberOfCellPoints; j++)
226 {
227 cell->GetPoints()->GetPoint(j,x);
228 for (i=0; i<3; i++)
229 {
230 jacobianMatrix[0][i] += x[i] * derivs[j];
231 jacobianMatrix[1][i] += x[i] * derivs[numberOfCellPoints+j];
232 jacobianMatrix[2][i] += x[i] * derivs[2*numberOfCellPoints+j];
233 }
234 }
235 delete[] derivs;
236
237 vtkMath::Invert3x3(jacobianMatrix,inverseJacobianMatrix);
238
239 vtkMath::Transpose3x3(inverseJacobianMatrix,inverseJacobianMatrix);
240}
241
242void vtkvmtkFEShapeFunctions::GetInterpolationFunctions(vtkCell* cell, double* pcoords, double* sf)
243{
244 switch (cell->GetCellType())
245 {
246 case VTK_QUAD:
247 vtkQuad::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
248 break;
249 case VTK_QUADRATIC_QUAD:
250 vtkQuadraticQuad::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
251 break;
252 case VTK_TRIANGLE:
253 vtkTriangle::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
254 break;
255 case VTK_QUADRATIC_TRIANGLE:
256 vtkQuadraticTriangle::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
257 break;
258 case VTK_HEXAHEDRON:
259 vtkHexahedron::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
260 break;
261 case VTK_QUADRATIC_HEXAHEDRON:
262 vtkQuadraticHexahedron::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
263 break;
264 case VTK_TRIQUADRATIC_HEXAHEDRON:
265 vtkTriQuadraticHexahedron::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
266 break;
267 case VTK_WEDGE:
268 vtkWedge::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
269 break;
270 case VTK_QUADRATIC_WEDGE:
271 vtkQuadraticWedge::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
272 break;
273 case VTK_BIQUADRATIC_QUADRATIC_WEDGE:
274 vtkBiQuadraticQuadraticWedge::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
275 break;
276 case VTK_TETRA:
277 vtkTetra::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
278 break;
279 case VTK_QUADRATIC_TETRA:
280 vtkQuadraticTetra::SafeDownCast(cell)->InterpolationFunctions(pcoords,sf);
281 break;
282 default:
283 vtkGenericWarningMacro("Error: unsupported cell type.");
284 return;
285 }
286}
287
288void vtkvmtkFEShapeFunctions::GetInterpolationDerivs(vtkCell* cell, double* pcoords, double* derivs)
289{
290 switch (cell->GetCellType())
291 {
292 case VTK_QUAD:
293 vtkQuad::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
294 break;
295 case VTK_QUADRATIC_QUAD:
296 vtkQuadraticQuad::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
297 break;
298 case VTK_TRIANGLE:
299 vtkTriangle::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
300 break;
301 case VTK_QUADRATIC_TRIANGLE:
302 vtkQuadraticTriangle::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
303 break;
304 case VTK_HEXAHEDRON:
305 vtkHexahedron::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
306 break;
307 case VTK_QUADRATIC_HEXAHEDRON:
308 vtkQuadraticHexahedron::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
309 break;
310 case VTK_TRIQUADRATIC_HEXAHEDRON:
311 vtkTriQuadraticHexahedron::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
312 break;
313 case VTK_WEDGE:
314 vtkWedge::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
315 break;
316 case VTK_QUADRATIC_WEDGE:
317 vtkQuadraticWedge::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
318 break;
319 case VTK_BIQUADRATIC_QUADRATIC_WEDGE:
320 vtkBiQuadraticQuadraticWedge::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
321 break;
322 case VTK_TETRA:
323 vtkTetra::SafeDownCast(cell)->InterpolateDerivs(pcoords,derivs);
324 break;
325 case VTK_QUADRATIC_TETRA:
326 vtkQuadraticTetra::SafeDownCast(cell)->InterpolationDerivs(pcoords,derivs);
327 break;
328 default:
329 vtkGenericWarningMacro("Error: unsupported cell type.");
330 return;
331 }
332}
333
334double vtkvmtkFEShapeFunctions::ComputeJacobian(vtkCell* cell, double* pcoords)
335{
336 double jacobian = 0.0;
337
338 int cellDimension = cell->GetCellDimension();
339
340 if (cellDimension == 2)
341 {
342 int numberOfCellPoints = cell->GetNumberOfPoints();
343 double* derivs = new double[2*numberOfCellPoints];
344
346
347 int i, j;
348
349 double jacobianMatrixTr[2][3];
350 for (i=0; i<3; i++)
351 {
352 jacobianMatrixTr[0][i] = jacobianMatrixTr[1][i] = 0.0;
353 }
354
355 double x[3];
356 for (j=0; j<numberOfCellPoints; j++)
357 {
358 cell->GetPoints()->GetPoint(j,x);
359 for (i=0; i<3; i++)
360 {
361 jacobianMatrixTr[0][i] += x[i] * derivs[j];
362 jacobianMatrixTr[1][i] += x[i] * derivs[numberOfCellPoints+j];
363 }
364 }
365 delete[] derivs;
366
367 double jacobianMatrixSquared[2][2];
368 jacobianMatrixSquared[0][0] = vtkMath::Dot(jacobianMatrixTr[0],jacobianMatrixTr[0]);
369 jacobianMatrixSquared[0][1] = vtkMath::Dot(jacobianMatrixTr[0],jacobianMatrixTr[1]);
370 jacobianMatrixSquared[1][0] = vtkMath::Dot(jacobianMatrixTr[1],jacobianMatrixTr[0]);
371 jacobianMatrixSquared[1][1] = vtkMath::Dot(jacobianMatrixTr[1],jacobianMatrixTr[1]);
372
373 double jacobianSquared = vtkMath::Determinant2x2(jacobianMatrixSquared[0],jacobianMatrixSquared[1]);
374
375 if (jacobianSquared < 0.0)
376 {
377#ifdef VTKVMTKFESHAPEFUNCTIONS_NEGATIVE_JACOBIAN_WARNING
378 vtkGenericWarningMacro("Warning: negative determinant of squared Jacobian, taking absolute value.");
379#endif
380 jacobianSquared = fabs(jacobianSquared);
381 }
382
383 jacobian = sqrt(jacobianSquared);
384 }
385 else if (cellDimension == 3)
386 {
387 int numberOfCellPoints = cell->GetNumberOfPoints();
388 double* derivs = new double[3*numberOfCellPoints];
389
391
392 int i, j;
393
394 double jacobianMatrix[3][3];
395 for (i=0; i<3; i++)
396 {
397 jacobianMatrix[0][i] = jacobianMatrix[1][i] = jacobianMatrix[2][i] = 0.0;
398 }
399
400 double x[3];
401 for (j=0; j<numberOfCellPoints; j++)
402 {
403 cell->GetPoints()->GetPoint(j,x);
404 for (i=0; i<3; i++)
405 {
406 jacobianMatrix[0][i] += x[i] * derivs[j];
407 jacobianMatrix[1][i] += x[i] * derivs[numberOfCellPoints+j];
408 jacobianMatrix[2][i] += x[i] * derivs[2*numberOfCellPoints+j];
409 }
410 }
411 delete[] derivs;
412
413 jacobian = vtkMath::Determinant3x3(jacobianMatrix);
414
415 if (jacobian < 0.0)
416 {
417#ifdef VTKVMTKFESHAPEFUNCTIONS_NEGATIVE_JACOBIAN_WARNING
418 vtkGenericWarningMacro("Warning: negative Jacobian, taking absolute value.");
419#endif
420 jacobian = fabs(jacobian);
421 }
422 }
423
424 return jacobian;
425}
426
Provides a set of common utility functions (calculate jacobian, interpolation function,...
void Initialize(vtkCell *cell, vtkDoubleArray *pcoords)
static void GetInterpolationDerivs(vtkCell *cell, double *pcoords, double *derivs)
static double ComputeJacobian(vtkCell *cell, double *pcoords)
static void ComputeInverseJacobianMatrix2D(vtkCell *cell, double *pcoords, double inverseJacobianMatrix[2][3])
static void ComputeInverseJacobianMatrix3D(vtkCell *cell, double *pcoords, double inverseJacobianMatrix[3][3])
static void GetInterpolationFunctions(vtkCell *cell, double *pcoords, double *sf)
vtkStandardNewMacro(vtkvmtkFEShapeFunctions)