| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "volumetricMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code author: Jernej Barbic * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * * |
| 11 | * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, * |
| 12 | * Doug L. James, Jovan Popovic * |
| 13 | * * |
| 14 | * Funding: National Science Foundation, Link Foundation, * |
| 15 | * Singapore-MIT GAMBIT Game Lab, * |
| 16 | * Zumberge Research and Innovation Fund at USC * |
| 17 | * * |
| 18 | * This library is free software; you can redistribute it and/or * |
| 19 | * modify it under the terms of the BSD-style license that is * |
| 20 | * included with this library in the file LICENSE.txt * |
| 21 | * * |
| 22 | * This library is distributed in the hope that it will be useful, * |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 25 | * LICENSE.TXT for more details. * |
| 26 | * * |
| 27 | *************************************************************************/ |
| 28 | |
| 29 | #include "tetMesh.h" |
| 30 | #include "volumetricMeshParser.h" |
| 31 | |
| 32 | const VolumetricMesh::elementType TetMesh::elementType_ = TET; |
| 33 | |
| 34 | TetMesh::TetMesh(const char * filename, fileFormatType fileFormat, int verbose) : VolumetricMesh(filename, fileFormat, 4, &temp, verbose) |
| 35 | { |
| 36 | if (temp != elementType_) |
| 37 | { |
| 38 | printf("Error: mesh is not a tet mesh.\n"); |
| 39 | throw 11; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | TetMesh::TetMesh(void * binaryStream, int memoryLoad) : VolumetricMesh(binaryStream, 4, &temp, memoryLoad) |
| 44 | { |
| 45 | if (temp != elementType_) |
| 46 | { |
| 47 | printf("Error: mesh is not a tet mesh.\n"); |
| 48 | throw 11; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | TetMesh::TetMesh(const char * filename, int specialFileType, int verbose): VolumetricMesh(4) |
| 53 | { |
| 54 | double E = 1E8; |
| 55 | double nu = 0.45; |
| 56 | double density = 1000; |
| 57 | |
| 58 | if (specialFileType != 0) |
| 59 | { |
| 60 | printf("Unknown special file type %d requested.\n", specialFileType); |
| 61 | throw 1; |
| 62 | } |
| 63 | |
| 64 | char lineBuffer[1024]; |
| 65 | VolumetricMeshParser parser; |
| 66 | |
| 67 | // first, read the vertices |
| 68 | sprintf(lineBuffer, "%s.node", filename); |
| 69 | if (parser.open(lineBuffer) != 0) |
| 70 | throw 2; |
| 71 | |
| 72 | parser.getNextLine(lineBuffer, 1); |
| 73 | int dim; |
| 74 | sscanf(lineBuffer, "%d %d", &numVertices, &dim); |
| 75 | if (dim != 3) |
| 76 | throw 3; |
| 77 | |
| 78 | vertices = new Vec3d [numVertices]; |
| 79 | |
| 80 | for(int i=0; i<numVertices; i++) |
| 81 | { |
| 82 | parser.getNextLine(lineBuffer, 1); |
| 83 | int index; |
| 84 | double x,y,z; |
| 85 | sscanf(lineBuffer, "%d %lf %lf %lf", &index, &x, &y, &z); |
| 86 | if (index != (i+1)) |
| 87 | throw 3; |
| 88 | vertices[i] = Vec3d(x,y,z); |
| 89 | } |
| 90 | |
| 91 | parser.close(); |
| 92 | |
| 93 | // next, read the elements |
| 94 | sprintf(lineBuffer, "%s.ele", filename); |
| 95 | if (parser.open(lineBuffer) != 0) |
| 96 | throw 4; |
| 97 | |
| 98 | parser.getNextLine(lineBuffer, 1); |
| 99 | sscanf(lineBuffer, "%d %d", &numElements, &dim); |
| 100 | if (dim != 4) |
| 101 | { |
| 102 | printf("Error: not a tet mesh file (%d vertices per tet encountered).\n", dim); |
| 103 | throw 5; |
| 104 | } |
| 105 | |
| 106 | elements = (int**) malloc (sizeof(int*) * numElements); |
| 107 | elementMaterial = (int*) malloc (sizeof(int) * numElements); |
| 108 | |
| 109 | for(int i=0; i<numElements; i++) |
| 110 | { |
| 111 | parser.getNextLine(lineBuffer, 1); |
| 112 | int index; |
| 113 | int v[4]; |
| 114 | sscanf(lineBuffer, "%d %d %d %d %d", &index, &v[0], &v[1], &v[2], &v[3]); |
| 115 | if (index != (i+1)) |
| 116 | throw 6; |
| 117 | elements[i] = (int*) malloc (sizeof(int) * 4); |
| 118 | for(int j=0; j<4; j++) // vertices are 1-indexed in .ele files |
| 119 | { |
| 120 | v[j]--; |
| 121 | elements[i][j] = v[j]; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | parser.close(); |
| 126 | |
| 127 | numMaterials = 0; |
| 128 | numSets = 0; |
| 129 | numRegions = 0; |
| 130 | materials = NULL; |
| 131 | sets = NULL; |
| 132 | regions = NULL; |
| 133 | |
| 134 | setSingleMaterial(E, nu, density); |
| 135 | } |
| 136 | |
| 137 | TetMesh::TetMesh(int numVertices_, double * vertices_, |
| 138 | int numElements_, int * elements_, |
| 139 | double E, double nu, double density): VolumetricMesh(numVertices_, vertices_, numElements_, 4, elements_, E, nu, density) {} |
| 140 | |
| 141 | TetMesh::TetMesh(int numVertices_, double * vertices_, |
| 142 | int numElements_, int * elements_, |
| 143 | int numMaterials_, Material ** materials_, |
| 144 | int numSets_, Set ** sets_, |
| 145 | int numRegions_, Region ** regions_): |
| 146 | VolumetricMesh(numVertices_, vertices_, numElements_, 4, elements_, |
| 147 | numMaterials_, materials_, numSets_, sets_, numRegions_, regions_) {} |
| 148 | |
| 149 | TetMesh::TetMesh(const TetMesh & source): VolumetricMesh(source) {} |
| 150 | |
| 151 | VolumetricMesh * TetMesh::clone() |
| 152 | { |
| 153 | TetMesh * mesh = new TetMesh(*this); |
| 154 | return mesh; |
| 155 | } |
| 156 | |
| 157 | TetMesh::TetMesh(const TetMesh & tetMesh, int numElements_, int * elements_, map<int,int> * vertexMap_): VolumetricMesh(tetMesh, numElements_, elements_, vertexMap_) {} |
| 158 | |
| 159 | TetMesh::~TetMesh() {} |
| 160 | |
| 161 | int TetMesh::saveToAscii(const char * filename) const |
| 162 | { |
| 163 | return VolumetricMesh::saveToAscii(filename, elementType_); |
| 164 | } |
| 165 | |
| 166 | int TetMesh::saveToBinary(const char * filename, unsigned int * bytesWritten) const |
| 167 | { |
| 168 | return VolumetricMesh::saveToBinary(filename, bytesWritten, elementType_); |
| 169 | } |
| 170 | |
| 171 | int TetMesh::saveToBinary(FILE * binaryOutputStream, unsigned int * bytesWritten, bool countBytesOnly) const |
| 172 | { |
| 173 | return VolumetricMesh::saveToBinary(binaryOutputStream, bytesWritten, elementType_, countBytesOnly); |
| 174 | } |
| 175 | |
| 176 | void TetMesh::computeElementMassMatrix(int el, double * massMatrix) const |
| 177 | { |
| 178 | /* |
| 179 | Consistent mass matrix of a tetrahedron = |
| 180 | |
| 181 | [ 2 1 1 1 ] |
| 182 | [ 1 2 1 1 ] |
| 183 | mass / 20 * [ 1 1 2 1 ] |
| 184 | [ 1 1 1 2 ] |
| 185 | |
| 186 | Note: mass = density * volume. Other than via the mass, the |
| 187 | consistent mass matrix does not depend on the shape of the tetrahedron. |
| 188 | (This can be seen after a long algebraic derivation; see: |
| 189 | Singiresu S. Rao: The finite element method in engineering, 2004) |
| 190 | */ |
| 191 | |
| 192 | const double mtx[16] = { 2, 1, 1, 1, |
| 193 | 1, 2, 1, 1, |
| 194 | 1, 1, 2, 1, |
| 195 | 1, 1, 1, 2 } ; |
| 196 | |
| 197 | double density = getElementDensity(el); |
| 198 | double factor = density * getElementVolume(el) / 20; |
| 199 | |
| 200 | for(int i=0; i<16; i++) |
| 201 | massMatrix[i] = factor * mtx[i]; |
| 202 | |
| 203 | // lumped mass |
| 204 | /* |
| 205 | double mass = element(el)->density() * getElementVolume(el); |
| 206 | massMatrix[0] = massMatrix[5] = massMatrix[10] = massMatrix[15] = mass / 4.0; |
| 207 | */ |
| 208 | } |
| 209 | |
| 210 | double TetMesh::getTetVolume(const Vec3d & a, const Vec3d & b, const Vec3d & c, const Vec3d & d) |
| 211 | { |
| 212 | // volume = 1/6 * | (d-a) . ((b-a) x (c-a)) | |
| 213 | return (1.0 / 6 * fabs(getTetDeterminant(a, b, c, d))); |
| 214 | } |
| 215 | |
| 216 | double TetMesh::getElementVolume(int el) const |
| 217 | { |
| 218 | const Vec3d & a = getVertex(el, 0); |
| 219 | const Vec3d & b = getVertex(el, 1); |
| 220 | const Vec3d & c = getVertex(el, 2); |
| 221 | const Vec3d & d = getVertex(el, 3); |
| 222 | return getTetVolume(a, b, c, d); |
| 223 | } |
| 224 | |
| 225 | void TetMesh::getElementInertiaTensor(int el, Mat3d & inertiaTensor) const |
| 226 | { |
| 227 | Vec3d a = getVertex(el, 0); |
| 228 | Vec3d b = getVertex(el, 1); |
| 229 | Vec3d c = getVertex(el, 2); |
| 230 | Vec3d d = getVertex(el, 3); |
| 231 | |
| 232 | Vec3d center = getElementCenter(el); |
| 233 | a -= center; |
| 234 | b -= center; |
| 235 | c -= center; |
| 236 | d -= center; |
| 237 | |
| 238 | double absdetJ = 6.0 * getElementVolume(el); |
| 239 | |
| 240 | double x1 = a[0], x2 = b[0], x3 = c[0], x4 = d[0]; |
| 241 | double y1 = a[1], y2 = b[1], y3 = c[1], y4 = d[1]; |
| 242 | double z1 = a[2], z2 = b[2], z3 = c[2], z4 = d[2]; |
| 243 | |
| 244 | double A = absdetJ * (y1*y1 + y1*y2 + y2*y2 + y1*y3 + y2*y3 + y3*y3 + y1*y4 + y2*y4 + y3*y4 + y4*y4 + z1*z1 + z1 * z2 + z2 * z2 + z1 * z3 + z2 * z3 + z3 * z3 + z1 * z4 + z2 * z4 + z3 * z4 + z4 * z4) / 60.0; |
| 245 | |
| 246 | double B = absdetJ * (x1*x1 + x1*x2 + x2*x2 + x1*x3 + x2*x3 + x3*x3 + x1*x4 + x2*x4 + x3*x4 + x4*x4 + z1*z1 + z1 * z2 + z2 * z2 + z1 * z3 + z2 * z3 + z3 * z3 + z1 * z4 + z2 * z4 + z3 * z4 + z4 * z4) / 60.0; |
| 247 | |
| 248 | double C = absdetJ * (x1*x1 + x1*x2 + x2*x2 + x1*x3 + x2*x3 + x3*x3 + x1*x4 + x2*x4 + x3*x4 + x4*x4 + y1*y1 + y1 * y2 + y2 * y2 + y1 * y3 + y2 * y3 + y3 * y3 + y1 * y4 + y2 * y4 + y3 * y4 + y4 * y4) / 60.0; |
| 249 | |
| 250 | double Ap = absdetJ * (2*y1*z1 + y2*z1 + y3*z1 + y4*z1 + y1*z2 + 2*y2*z2 + y3*z2 + y4*z2 + y1*z3 + y2*z3 + 2*y3*z3 + y4*z3 + y1*z4 + y2*z4 + y3*z4 + 2*y4*z4) / 120.0; |
| 251 | |
| 252 | double Bp = absdetJ * (2*x1*z1 + x2*z1 + x3*z1 + x4*z1 + x1*z2 + 2*x2*z2 + x3*z2 + x4*z2 + x1*z3 + x2*z3 + 2*x3*z3 + x4*z3 + x1*z4 + x2*z4 + x3*z4 + 2*x4*z4) / 120.0; |
| 253 | |
| 254 | double Cp = absdetJ * (2*x1*y1 + x2*y1 + x3*y1 + x4*y1 + x1*y2 + 2*x2*y2 + x3*y2 + x4*y2 + x1*y3 + x2*y3 + 2*x3*y3 + x4*y3 + x1*y4 + x2*y4 + x3*y4 + 2*x4*y4) / 120.0; |
| 255 | |
| 256 | inertiaTensor = Mat3d(A, -Bp, -Cp, -Bp, B, -Ap, -Cp, -Ap, C); |
| 257 | } |
| 258 | |
| 259 | bool TetMesh::containsVertex(int el, Vec3d pos) const // true if given element contain given position, false otherwise |
| 260 | { |
| 261 | double weights[4]; |
| 262 | computeBarycentricWeights(el, pos, weights); |
| 263 | |
| 264 | // all weights must be non-negative |
| 265 | return ((weights[0] >= 0) && (weights[1] >= 0) && (weights[2] >= 0) && (weights[3] >= 0)); |
| 266 | } |
| 267 | |
| 268 | void TetMesh::computeBarycentricWeights(const Vec3d vertexPos[4], const Vec3d & pos, double * weights) |
| 269 | { |
| 270 | /* |
| 271 | |x1 y1 z1 1| |
| 272 | D0 = |x2 y2 z2 1| |
| 273 | |x3 y3 z3 1| |
| 274 | |x4 y4 z4 1| |
| 275 | |
| 276 | |x y z 1| |
| 277 | D1 = |x2 y2 z2 1| |
| 278 | |x3 y3 z3 1| |
| 279 | |x4 y4 z4 1| |
| 280 | |
| 281 | |x1 y1 z1 1| |
| 282 | D2 = |x y z 1| |
| 283 | |x3 y3 z3 1| |
| 284 | |x4 y4 z4 1| |
| 285 | |
| 286 | |x1 y1 z1 1| |
| 287 | D3 = |x2 y2 z2 1| |
| 288 | |x y z 1| |
| 289 | |x4 y4 z4 1| |
| 290 | |
| 291 | |x1 y1 z1 1| |
| 292 | D4 = |x2 y2 z2 1| |
| 293 | |x3 y3 z3 1| |
| 294 | |x y z 1| |
| 295 | |
| 296 | wi = Di / D0 |
| 297 | */ |
| 298 | |
| 299 | double D[5]; |
| 300 | D[0] = getTetDeterminant(vertexPos[0], vertexPos[1], vertexPos[2], vertexPos[3]); |
| 301 | |
| 302 | for(int i=1; i<=4; i++) |
| 303 | { |
| 304 | Vec3d buf[4]; |
| 305 | for(int j=0; j<4; j++) |
| 306 | buf[j] = vertexPos[j]; |
| 307 | buf[i-1] = pos; |
| 308 | D[i] = getTetDeterminant(buf[0], buf[1], buf[2], buf[3]); |
| 309 | weights[i-1] = D[i] / D[0]; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void TetMesh::computeBarycentricWeights(int el, const Vec3d & pos, double * weights) const |
| 314 | { |
| 315 | Vec3d vtx[4]; |
| 316 | for(int i=0; i<4; i++) |
| 317 | vtx[i] = getVertex(el,i); |
| 318 | |
| 319 | computeBarycentricWeights(vtx, pos, weights); |
| 320 | } |
| 321 | |
| 322 | double TetMesh::getTetDeterminant(const Vec3d & a, const Vec3d & b, const Vec3d & c, const Vec3d & d) |
| 323 | { |
| 324 | // computes det(A), for the 4x4 matrix A |
| 325 | // [ 1 a ] |
| 326 | // A = [ 1 b ] |
| 327 | // [ 1 c ] |
| 328 | // [ 1 d ] |
| 329 | // It can be shown that det(A) = dot(d - a, cross(b - a, c - a)) |
| 330 | // When det(A) > 0, the tet has positive orientation. |
| 331 | // When det(A) = 0, the tet is degenerate. |
| 332 | // When det(A) < 0, the tet has negative orientation. |
| 333 | |
| 334 | return dot(d - a, cross(b - a, c - a)); |
| 335 | } |
| 336 | |
| 337 | void TetMesh::interpolateGradient(int element, const double * U, int numFields, Vec3d pos, double * grad) const |
| 338 | { |
| 339 | computeGradient(element, U, numFields, grad); |
| 340 | } |
| 341 | |
| 342 | void TetMesh::computeGradient(int element, const double * U, int numFields, double * grad) const |
| 343 | { |
| 344 | // grad is 9 x numFields |
| 345 | // grad is constant inside a tet |
| 346 | Vec3d vtx[4]; |
| 347 | for(int i=0; i<4; i++) |
| 348 | vtx[i] = getVertex(element,i); |
| 349 | |
| 350 | // form M = |
| 351 | // [b - a] |
| 352 | // [c - a] |
| 353 | // [d - a] |
| 354 | |
| 355 | Mat3d M(vtx[1] - vtx[0], vtx[2] - vtx[0], vtx[3] - vtx[0]); |
| 356 | Mat3d MInv = inv(M); |
| 357 | //printf("M=\n"); |
| 358 | //M.print(); |
| 359 | |
| 360 | for(int field=0; field<numFields; field++) |
| 361 | { |
| 362 | // form rhs = |
| 363 | // [U1 - U0] |
| 364 | // [U2 - U0] |
| 365 | // [U3 - U0] |
| 366 | const double * u[4]; |
| 367 | for(int i=0; i<4; i++) |
| 368 | u[i] = &U[3 * numVertices * field + 3 * getVertexIndex(element, i)]; |
| 369 | |
| 370 | Vec3d rows[3]; |
| 371 | for(int i=0; i<3; i++) |
| 372 | rows[i] = Vec3d(u[i+1]) - Vec3d(u[0]); |
| 373 | |
| 374 | Mat3d rhs(rows); |
| 375 | //printf("rhs=\n"); |
| 376 | //rhs.print(); |
| 377 | |
| 378 | Mat3d gradM = trans(MInv * rhs); |
| 379 | gradM.convertToArray(&grad[9 * field]); |
| 380 | |
| 381 | /* |
| 382 | // test gradient |
| 383 | if (field == 0) |
| 384 | { |
| 385 | printf("----\n"); |
| 386 | printf("0: pos: %.15f %.15f %.15f | uExact: %.15f %.15f %.15f\n", vtx[0][0], vtx[0][1], vtx[0][2], u[0][0], u[0][1], u[0][2]); |
| 387 | for(int vertex=0; vertex<3; vertex++) |
| 388 | { |
| 389 | Vec3d u1 = Vec3d(u[vertex+1]); |
| 390 | printf("%d: ", vertex+1); |
| 391 | printf("pos: %.15f %.15f %.15f | uExact: %.15f %.15f %.15f | ", vtx[vertex+1][0], vtx[vertex+1][1], vtx[vertex+1][2], u1[0], u1[1], u1[2]); |
| 392 | Vec3d u1approx = Vec3d(u[0]) + gradM * (vtx[vertex+1] - vtx[0]); |
| 393 | printf("uApprox: %.15f %.15f %.15f\n", u1approx[0], u1approx[1], u1approx[2]); |
| 394 | } |
| 395 | printf("----\n"); |
| 396 | } |
| 397 | */ |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | |
| 403 | int TetMesh::getNumElementEdges() const |
| 404 | { |
| 405 | return 6; |
| 406 | } |
| 407 | |
| 408 | void TetMesh::getElementEdges(int el, int * edgeBuffer) const |
| 409 | { |
| 410 | int v[4]; |
| 411 | for(int i=0; i<4; i++) |
| 412 | v[i] = getVertexIndex(el,i); |
| 413 | |
| 414 | int edgeMask[6][2] = { |
| 415 | { 0, 1 }, { 1, 2 }, { 2, 0 }, |
| 416 | { 0, 3 }, { 1, 3 }, { 2, 3 } }; |
| 417 | |
| 418 | for(int edge=0; edge<6; edge++) |
| 419 | { |
| 420 | edgeBuffer[2*edge+0] = v[edgeMask[edge][0]]; |
| 421 | edgeBuffer[2*edge+1] = v[edgeMask[edge][1]]; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void TetMesh::orient() |
| 426 | { |
| 427 | for(int el=0; el<numElements; el++) |
| 428 | { |
| 429 | // a, b, c, d |
| 430 | // dot(d - a, cross(b - a, c - a)) |
| 431 | double det = getTetDeterminant(getVertex(el, 0), getVertex(el, 1), getVertex(el, 2), getVertex(el, 3)); |
| 432 | |
| 433 | if (det < 0) |
| 434 | { |
| 435 | // reverse tet |
| 436 | int * elementVertices = elements[el]; |
| 437 | // swap 2 and 3 |
| 438 | int buff = elementVertices[2]; |
| 439 | elementVertices[2] = elementVertices[3]; |
| 440 | elementVertices[3] = buff; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 |