| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "StVK" 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 "StVKTetHighMemoryABCD.h" |
| 30 | |
| 31 | //#define CONTIGUOUSBLOCK |
| 32 | |
| 33 | StVKTetHighMemoryABCD::StVKTetHighMemoryABCD(TetMesh * tetMesh) |
| 34 | { |
| 35 | int numElements = tetMesh->getNumElements(); |
| 36 | |
| 37 | // allocate contiguous buffer for coefficients |
| 38 | int totalCoefficientSize = (sizeof(Mat3d [4][4]) + |
| 39 | sizeof(double [4][4]) + |
| 40 | sizeof(Vec3d [4][4][4]) + |
| 41 | sizeof(double [4][4][4][4]) ) * numElements; |
| 42 | |
| 43 | #ifdef CONTIGUOUSBLOCK |
| 44 | unsigned char * block = (unsigned char*) malloc (sizeof(unsigned char) * totalCoefficientSize); |
| 45 | A_ = (Mat3d (*) [4][4]) &block[0]; |
| 46 | B_ = (double (*) [4][4]) &block[sizeof(Mat3d [4][4]) * numElements]; |
| 47 | C_ = (Vec3d (*) [4][4][4]) &block[(sizeof(Mat3d [4][4]) + sizeof(double [4][4])) * numElements]; |
| 48 | D_ = (double (*) [4][4][4][4]) &block[(sizeof(Mat3d [4][4]) + sizeof(double [4][4]) + sizeof(Vec3d [4][4][4])) * numElements]; |
| 49 | #else |
| 50 | A_ = (Mat3d (*) [4][4]) malloc (sizeof(Mat3d [4][4]) * numElements); |
| 51 | B_ = (double (*) [4][4]) malloc (sizeof(double [4][4]) * numElements); |
| 52 | C_ = (Vec3d (*) [4][4][4]) malloc (sizeof(Vec3d [4][4][4]) * numElements); |
| 53 | D_ = (double (*) [4][4][4][4]) malloc (sizeof(double [4][4][4][4]) * numElements); |
| 54 | #endif |
| 55 | |
| 56 | for(int el=0; el<numElements; el++) |
| 57 | { |
| 58 | Vec3d vertices[4]; |
| 59 | for(int i=0; i<4; i++) |
| 60 | vertices[i] = tetMesh->getVertex(el, i); |
| 61 | StVKSingleTetABCD(vertices, A_[el], B_[el], C_[el], D_[el]); |
| 62 | } |
| 63 | |
| 64 | printf("Total tet ABCD coefficient size: %G Mb.\n", |
| 65 | 1.0 * totalCoefficientSize / 1024 / 1024); |
| 66 | } |
| 67 | |
| 68 | StVKTetHighMemoryABCD::~StVKTetHighMemoryABCD() |
| 69 | { |
| 70 | #ifdef CONTIGUOUSBLOCK |
| 71 | free((unsigned char*)A_); |
| 72 | #else |
| 73 | free(A_); |
| 74 | free(B_); |
| 75 | free(C_); |
| 76 | free(D_); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | void StVKTetHighMemoryABCD::StVKSingleTetABCD(Vec3d vtx[4], Mat3d A[4][4], double B[4][4], Vec3d C[4][4][4], double D[4][4][4][4]) |
| 81 | { |
| 82 | double det = TetMesh::getTetDeterminant(vtx[0], vtx[1], vtx[2], vtx[3]); |
| 83 | double volume = fabs(det / 6); |
| 84 | |
| 85 | Vec3d Phig[4]; |
| 86 | for(int i=0; i<4; i++) |
| 87 | for(int j=0; j<3; j++) |
| 88 | { |
| 89 | Vec3d columns[2]; |
| 90 | int countI = 0; |
| 91 | for(int ii=0; ii<4; ii++) |
| 92 | { |
| 93 | if (ii == i) |
| 94 | continue; |
| 95 | int countJ = 0; |
| 96 | for(int jj=0; jj<3; jj++) |
| 97 | { |
| 98 | if (jj == j) |
| 99 | continue; |
| 100 | |
| 101 | columns[countJ][countI] = vtx[ii][jj]; |
| 102 | countJ++; |
| 103 | } |
| 104 | int sign = (((i + j) % 2) == 0) ? -1 : 1; |
| 105 | Phig[i][j] = 1.0 * sign * dot(Vec3d(1,1,1), cross(columns[0],columns[1])) / det; |
| 106 | countI++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // A |
| 111 | for (int i=0; i<4; i++) |
| 112 | for (int j=0; j<4; j++) |
| 113 | A[i][j] = volume * tensorProduct(Phig[i], Phig[j]); |
| 114 | |
| 115 | // B |
| 116 | for (int i=0; i<4; i++) |
| 117 | for (int j=0; j<4; j++) |
| 118 | B[i][j] = volume * dot(Phig[i], Phig[j]); |
| 119 | |
| 120 | // C |
| 121 | for (int i=0; i<4; i++) |
| 122 | for (int j=0; j<4; j++) |
| 123 | for (int k=0; k<4; k++) |
| 124 | C[i][j][k] = volume * dot(Phig[j], Phig[k]) * Phig[i]; |
| 125 | |
| 126 | // D |
| 127 | for (int i=0; i<4; i++) |
| 128 | for (int j=0; j<4; j++) |
| 129 | for (int k=0; k<4; k++) |
| 130 | for (int l=0; l<4; l++) |
| 131 | D[i][j][k][l] = volume * dot(Phig[i],Phig[j]) * dot(Phig[k],Phig[l]); |
| 132 | } |
| 133 |