| 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 "StVKHessianTensor.h" |
| 30 | #include "volumetricMeshENuMaterial.h" |
| 31 | |
| 32 | #define QUADRATICFORM(m,x,y)\ |
| 33 | m[0] * x[0] * y[0] + \ |
| 34 | m[1] * x[1] * y[0] + \ |
| 35 | m[2] * x[2] * y[0] + \ |
| 36 | m[3] * x[0] * y[1] + \ |
| 37 | m[4] * x[1] * y[1] + \ |
| 38 | m[5] * x[2] * y[1] + \ |
| 39 | m[6] * x[0] * y[2] + \ |
| 40 | m[7] * x[1] * y[2] + \ |
| 41 | m[8] * x[2] * y[2] |
| 42 | |
| 43 | #define TENSOR3_INDEX(i,j,k) (9*(i)+3*(j)+(k)) |
| 44 | |
| 45 | #define ADD_CONTRIB(aa,bb,cc)\ |
| 46 | v1 = vertices[(aa)];\ |
| 47 | v2 = vertices[(bb)];\ |
| 48 | v3 = vertices[(cc)];\ |
| 49 | phirj = &phir[3*v2];\ |
| 50 | phisk = &phis[3*v3];\ |
| 51 | result[3*v1+0] += QUADRATICFORM(hijk0,phisk,phirj);\ |
| 52 | result[3*v1+1] += QUADRATICFORM(hijk1,phisk,phirj);\ |
| 53 | result[3*v1+2] += QUADRATICFORM(hijk2,phisk,phirj); |
| 54 | |
| 55 | #define ADD_CONTRIB_ALL(aa,bb,cc)\ |
| 56 | v1 = vertices[(aa)];\ |
| 57 | v2 = vertices[(bb)];\ |
| 58 | v3 = vertices[(cc)];\ |
| 59 | for(int ii=0; ii<k-numRigidModes; ii++)\ |
| 60 | for(int jj=ii; jj<k-numRigidModes; jj++)\ |
| 61 | {\ |
| 62 | int derivPos = ii * (k-numRigidModes) - ii * (ii+1) / 2 + jj;\ |
| 63 | phirj = &Ulin[m3*(numRigidModes+ii) + 3*v2];\ |
| 64 | phisk = &Ulin[m3*(numRigidModes+jj) + 3*v3];\ |
| 65 | result[m3*derivPos + 3*v1+0] += QUADRATICFORM(hijk0,phisk,phirj);\ |
| 66 | result[m3*derivPos + 3*v1+1] += QUADRATICFORM(hijk1,phisk,phirj);\ |
| 67 | result[m3*derivPos + 3*v1+2] += QUADRATICFORM(hijk2,phisk,phirj);\ |
| 68 | } |
| 69 | |
| 70 | StVKHessianTensor::StVKHessianTensor(StVKStiffnessMatrix * stVKStiffnessMatrix_): stVKStiffnessMatrix(stVKStiffnessMatrix_), volumetricMesh(stVKStiffnessMatrix->GetVolumetricMesh()), precomputedIntegrals(stVKStiffnessMatrix->GetPrecomputedIntegrals()) |
| 71 | { |
| 72 | numVertices_ = volumetricMesh->getNumVertices(); |
| 73 | numElements_ = volumetricMesh->getNumElements(); |
| 74 | numElementVertices = volumetricMesh->getNumElementVertices(); |
| 75 | |
| 76 | lambdaLame = (double*) malloc (sizeof(double) * numElements_); |
| 77 | muLame = (double*) malloc (sizeof(double) * numElements_); |
| 78 | |
| 79 | for(int el=0; el<numElements_; el++) |
| 80 | { |
| 81 | VolumetricMesh::Material * material = volumetricMesh->getElementMaterial(el); |
| 82 | VolumetricMesh::ENuMaterial * eNuMaterial = downcastENuMaterial(material); |
| 83 | if (eNuMaterial == NULL) |
| 84 | { |
| 85 | printf("Error: StVKHessianTensor: mesh does not consist of E, nu materials.\n"); |
| 86 | throw 1; |
| 87 | } |
| 88 | |
| 89 | lambdaLame[el] = eNuMaterial->getLambda(); |
| 90 | muLame[el] = eNuMaterial->getMu(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | StVKHessianTensor::~StVKHessianTensor() |
| 95 | { |
| 96 | free(lambdaLame); |
| 97 | free(muLame); |
| 98 | } |
| 99 | |
| 100 | void StVKHessianTensor::AddTensor3x3x3Block(int v1, int v2, int v3, Vec3d & vec, int type) |
| 101 | { |
| 102 | |
| 103 | #define TENSOR3_INDEX(i,j,k) (9*(i)+3*(j)+(k)) |
| 104 | |
| 105 | // seek for the Hessian term |
| 106 | double * entry = NULL; |
| 107 | |
| 108 | hessianType::iterator pos; |
| 109 | pos = hessian.find(triIndex(v1,v2,v3)); |
| 110 | |
| 111 | if (pos == hessian.end()) |
| 112 | { |
| 113 | // data is new |
| 114 | entry = (double*) calloc (27,sizeof(double)); |
| 115 | hessian.insert(std::make_pair(triIndex(v1,v2,v3),entry)); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | // data already exists |
| 120 | entry = pos->second; |
| 121 | } |
| 122 | |
| 123 | // modify entry as necessary |
| 124 | |
| 125 | switch (type) |
| 126 | { |
| 127 | case 1: // vec tensor u |
| 128 | |
| 129 | for(int j=0; j<3; j++) |
| 130 | for(int i=0; i<3; i++) |
| 131 | entry[TENSOR3_INDEX(i,j,j)] += vec[i]; |
| 132 | |
| 133 | break; |
| 134 | |
| 135 | case 2: // u tensor vec |
| 136 | |
| 137 | for(int j=0; j<3; j++) |
| 138 | for(int i=0; i<3; i++) |
| 139 | entry[TENSOR3_INDEX(j,i,j)] += vec[i]; |
| 140 | |
| 141 | break; |
| 142 | |
| 143 | case 3: // <u,vec>I |
| 144 | |
| 145 | for(int j=0; j<3; j++) |
| 146 | for(int i=0; i<3; i++) |
| 147 | entry[TENSOR3_INDEX(j,j,i)] += vec[i]; |
| 148 | |
| 149 | break; |
| 150 | |
| 151 | default: |
| 152 | printf("Error: type with tensor3 invalid.\n"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | int StVKHessianTensor::ComputeHessianAtZero(int verbose) |
| 158 | { |
| 159 | if (verbose) |
| 160 | printf("Computing Hessian at zero... Total num elements: %d \n", numElements_); |
| 161 | |
| 162 | int * vertices = (int*) malloc (sizeof(int) * numElementVertices); |
| 163 | |
| 164 | void * elIter; |
| 165 | precomputedIntegrals->AllocateElementIterator(&elIter); |
| 166 | |
| 167 | hessian.clear(); |
| 168 | |
| 169 | for(int el=0; el < numElements_; el++) |
| 170 | { |
| 171 | precomputedIntegrals->PrepareElement(el, elIter); |
| 172 | |
| 173 | if (verbose && (el % 100 == 1)) |
| 174 | { |
| 175 | printf("%d ",el); |
| 176 | fflush(NULL); |
| 177 | } |
| 178 | |
| 179 | for(int ver=0; ver<numElementVertices ;ver++) |
| 180 | vertices[ver] = volumetricMesh->getVertexIndex(el, ver); |
| 181 | |
| 182 | double lambda = lambdaLame[el]; |
| 183 | double mu = muLame[el]; |
| 184 | |
| 185 | for (int c=0; c<numElementVertices; c++) // over all vertices of the voxel, computing force on vertex c |
| 186 | { |
| 187 | // quadratic terms |
| 188 | int a,b; |
| 189 | for (a=0; a<numElementVertices; a++) // over all vertices |
| 190 | for(b=0; b<numElementVertices; b++) |
| 191 | { |
| 192 | Vec3d D = 0.5 * lambda * precomputedIntegrals->C(elIter,c,a,b) + |
| 193 | mu * precomputedIntegrals->C(elIter,a,b,c); |
| 194 | |
| 195 | // tensorProduct(D,qb); |
| 196 | AddTensor3x3x3Block(vertices[c], vertices[a], vertices[b], D, 1); |
| 197 | |
| 198 | // tensorProduct(D,qa); |
| 199 | AddTensor3x3x3Block(vertices[c], vertices[b], vertices[a], D, 1); |
| 200 | |
| 201 | Vec3d C = lambda * precomputedIntegrals->C(elIter,a,b,c) + |
| 202 | mu * (precomputedIntegrals->C(elIter,c,a,b) + precomputedIntegrals->C(elIter,b,a,c)); |
| 203 | |
| 204 | // tensorProduct(qb,C); |
| 205 | AddTensor3x3x3Block(vertices[c], vertices[a], vertices[b], C, 2); |
| 206 | |
| 207 | // <qa,C> I |
| 208 | AddTensor3x3x3Block(vertices[c], vertices[b], vertices[a], C, 3); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | free(vertices); |
| 214 | |
| 215 | precomputedIntegrals->ReleaseElementIterator(elIter); |
| 216 | |
| 217 | if (verbose) |
| 218 | printf("\n"); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | void StVKHessianTensor::EvaluateHessianQuadraticForm(double * phir, double * phis, double * result) |
| 224 | { |
| 225 | // reset result to zero |
| 226 | memset(result,0,sizeof(double)*3*numVertices_); |
| 227 | |
| 228 | int i,j,k; |
| 229 | |
| 230 | hessianType::iterator pos; |
| 231 | |
| 232 | for (pos = hessian.begin(); pos != hessian.end(); pos++) |
| 233 | { |
| 234 | triIndex index = pos->first; |
| 235 | i = index.first; |
| 236 | j = index.second; |
| 237 | k = index.third; |
| 238 | double * entry = pos->second; |
| 239 | |
| 240 | double * phisk = &phis[3*k]; |
| 241 | double * phirj = &phir[3*j]; |
| 242 | |
| 243 | double * hijk0 = &entry[0]; |
| 244 | double * hijk1 = &entry[9]; |
| 245 | double * hijk2 = &entry[18]; |
| 246 | |
| 247 | // compute quadratic forms |
| 248 | double value0 = QUADRATICFORM(hijk0,phisk,phirj); |
| 249 | double value1 = QUADRATICFORM(hijk1,phisk,phirj); |
| 250 | double value2 = QUADRATICFORM(hijk2,phisk,phirj); |
| 251 | |
| 252 | result[3*i+0] += value0; |
| 253 | result[3*i+1] += value1; |
| 254 | result[3*i+2] += value2; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | int StVKHessianTensor::SaveHessianAtZeroToFile(const char * filename) |
| 259 | { |
| 260 | FILE * fout = fopen(filename,"wb"); |
| 261 | |
| 262 | if (!fout) |
| 263 | { |
| 264 | printf("Error: couldn't write to file %s.\n",filename); |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | size_t hessianSize = hessian.size(); |
| 269 | if ((int)(fwrite(&hessianSize,sizeof(int),1,fout)) < 1) |
| 270 | return 1; |
| 271 | |
| 272 | hessianType::iterator pos; |
| 273 | |
| 274 | for (pos = hessian.begin(); pos != hessian.end(); pos++) |
| 275 | { |
| 276 | triIndex index = pos->first; |
| 277 | int data0 = index.first; |
| 278 | int data1 = index.second; |
| 279 | int data2 = index.third; |
| 280 | |
| 281 | if ((int)(fwrite(&data0,sizeof(int),1,fout)) < 1) |
| 282 | return 1; |
| 283 | |
| 284 | if ((int)(fwrite(&data1,sizeof(int),1,fout)) < 1) |
| 285 | return 1; |
| 286 | |
| 287 | if ((int)(fwrite(&data2,sizeof(int),1,fout)) < 1) |
| 288 | return 1; |
| 289 | |
| 290 | double * entry = pos->second; |
| 291 | for(int j=0; j<27; j++) |
| 292 | { |
| 293 | double entry0 = entry[j]; |
| 294 | if ((int)(fwrite(&entry0,sizeof(double),1,fout)) < 1) |
| 295 | return 1; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fclose(fout); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | void StVKHessianTensor::EvaluateHessianQuadraticFormDirect(double * phir, double * phis, double * result) |
| 305 | { |
| 306 | double entry[27]; |
| 307 | double * hijk0 = &entry[0]; |
| 308 | double * hijk1 = &entry[9]; |
| 309 | double * hijk2 = &entry[18]; |
| 310 | |
| 311 | double * phisk; |
| 312 | double * phirj; |
| 313 | |
| 314 | int v1,v2,v3; |
| 315 | |
| 316 | int i,j; |
| 317 | |
| 318 | // reset result to zero |
| 319 | memset(result,0,sizeof(double)*3*numVertices_); |
| 320 | |
| 321 | int * vertices = (int*) malloc (sizeof(int) * numElementVertices); |
| 322 | |
| 323 | void * elIter; |
| 324 | precomputedIntegrals->AllocateElementIterator(&elIter); |
| 325 | |
| 326 | printf("Evaluating Hessian Quadratic Form (rhs matrix)... Total num elements: %d \n",numElements_); |
| 327 | |
| 328 | for(int el=0; el < numElements_; el++) |
| 329 | { |
| 330 | precomputedIntegrals->PrepareElement(el, elIter); |
| 331 | if (el % 500 == 1) |
| 332 | printf("%d ",el); |
| 333 | |
| 334 | for(int ver=0; ver<numElementVertices; ver++) |
| 335 | vertices[ver] = volumetricMesh->getVertexIndex(el, ver); |
| 336 | |
| 337 | double lambda = lambdaLame[el]; |
| 338 | double mu = muLame[el]; |
| 339 | |
| 340 | for (int c=0; c<numElementVertices; c++) // over all vertices of the voxel, computing force on vertex c |
| 341 | { |
| 342 | // quadratic terms |
| 343 | int a,b; |
| 344 | for (a=0; a<numElementVertices; a++) // over all vertices |
| 345 | for(b=0; b<numElementVertices; b++) |
| 346 | { |
| 347 | Vec3d D = 0.5 * lambda * precomputedIntegrals->C(elIter,c,a,b) + |
| 348 | mu * precomputedIntegrals->C(elIter,a,b,c); |
| 349 | |
| 350 | // tensorProduct(D,qb); |
| 351 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[a], vertices[b], D, 1, phir, phis, result); |
| 352 | |
| 353 | // tensorProduct(D,qa); |
| 354 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[b], vertices[a], D, 1, phir, phis, result); |
| 355 | |
| 356 | memset(entry,0,sizeof(double)*27); |
| 357 | for(j=0; j<3; j++) |
| 358 | for(i=0; i<3; i++) |
| 359 | entry[TENSOR3_INDEX(i,j,j)] += D[i]; |
| 360 | |
| 361 | ADD_CONTRIB(c,a,b); |
| 362 | ADD_CONTRIB(c,b,a); |
| 363 | |
| 364 | //------- |
| 365 | |
| 366 | Vec3d C = lambda * precomputedIntegrals->C(elIter,a,b,c) + |
| 367 | mu * (precomputedIntegrals->C(elIter,c,a,b) + precomputedIntegrals->C(elIter,b,a,c)); |
| 368 | |
| 369 | // tensorProduct(qb,C); |
| 370 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[a], vertices[b], C, 2, phir, phis, result); |
| 371 | |
| 372 | memset(entry,0,sizeof(double)*27); |
| 373 | for(j=0; j<3; j++) |
| 374 | for(i=0; i<3; i++) |
| 375 | entry[TENSOR3_INDEX(j,i,j)] += C[i]; |
| 376 | |
| 377 | ADD_CONTRIB(c,a,b); |
| 378 | |
| 379 | //------- |
| 380 | |
| 381 | // <qa,C> I |
| 382 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[b], vertices[a], C, 3, phir, phis, result); |
| 383 | |
| 384 | memset(entry,0,sizeof(double)*27); |
| 385 | for(j=0; j<3; j++) |
| 386 | for(i=0; i<3; i++) |
| 387 | entry[TENSOR3_INDEX(j,j,i)] += C[i]; |
| 388 | |
| 389 | ADD_CONTRIB(c,b,a); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | free(vertices); |
| 395 | |
| 396 | precomputedIntegrals->ReleaseElementIterator(elIter); |
| 397 | |
| 398 | printf("\n"); |
| 399 | } |
| 400 | |
| 401 | void StVKHessianTensor::EvaluateHessianQuadraticFormDirectAll(double * Ulin, int k, double * result, int numRigidModes, int verbose) |
| 402 | { |
| 403 | double entry[27]; |
| 404 | double * hijk0 = &entry[0]; |
| 405 | double * hijk1 = &entry[9]; |
| 406 | double * hijk2 = &entry[18]; |
| 407 | |
| 408 | double * phisk; |
| 409 | double * phirj; |
| 410 | |
| 411 | int v1,v2,v3; |
| 412 | |
| 413 | // reset result to zero |
| 414 | int numDeriv = (k-numRigidModes) * (k-numRigidModes+1) / 2; |
| 415 | int m3 = 3*numVertices_; |
| 416 | |
| 417 | memset(result,0,sizeof(double)*m3*numDeriv); |
| 418 | |
| 419 | int * vertices = (int*) malloc (sizeof(int) * numElementVertices); |
| 420 | |
| 421 | void * elIter; |
| 422 | precomputedIntegrals->AllocateElementIterator(&elIter); |
| 423 | |
| 424 | if (verbose) |
| 425 | { |
| 426 | printf("Evaluating the Hessian quadratic form (rhs matrix)...\n"); |
| 427 | printf(" Total num elements: %d \n",numElements_); |
| 428 | printf(" Total num DOFs: %d \n",m3); |
| 429 | printf(" Total num linear modes: %d \n",k); |
| 430 | printf(" Total num rigid modes: %d \n",numRigidModes); |
| 431 | printf(" Total num derivatives: %d \n",numDeriv); |
| 432 | } |
| 433 | |
| 434 | for(int el=0; el < numElements_; el++) |
| 435 | { |
| 436 | precomputedIntegrals->PrepareElement(el, elIter); |
| 437 | if ((el % 200 == 0) && (verbose)) |
| 438 | { |
| 439 | printf("%d ",el); |
| 440 | fflush(NULL); |
| 441 | } |
| 442 | for(int ver=0; ver<numElementVertices; ver++) |
| 443 | vertices[ver] = volumetricMesh->getVertexIndex(el, ver); |
| 444 | |
| 445 | double lambda = lambdaLame[el]; |
| 446 | double mu = muLame[el]; |
| 447 | |
| 448 | for (int c=0; c<numElementVertices; c++) // over all vertices of the voxel, computing force on vertex c |
| 449 | { |
| 450 | // quadratic terms |
| 451 | for (int a=0; a<numElementVertices; a++) // over all vertices |
| 452 | for(int b=0; b<numElementVertices; b++) |
| 453 | { |
| 454 | Vec3d D = lambda * precomputedIntegrals->C(elIter,c,a,b) + |
| 455 | mu * (precomputedIntegrals->C(elIter,a,b,c) + precomputedIntegrals->C(elIter,b,a,c)); |
| 456 | |
| 457 | // tensorProduct(D,qb); |
| 458 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[a], vertices[b], D, 1, phir, phis, result); |
| 459 | |
| 460 | // tensorProduct(D,qa); |
| 461 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[b], vertices[a], D, 1, phir, phis, result); |
| 462 | |
| 463 | memset(entry,0,sizeof(double)*27); |
| 464 | for(int j=0; j<3; j++) |
| 465 | for(int i=0; i<3; i++) |
| 466 | entry[TENSOR3_INDEX(i,j,j)] += D[i]; |
| 467 | |
| 468 | ADD_CONTRIB_ALL(c,a,b); |
| 469 | |
| 470 | //------- |
| 471 | |
| 472 | Vec3d C = lambda * precomputedIntegrals->C(elIter,a,b,c) + |
| 473 | mu * (precomputedIntegrals->C(elIter,c,a,b) + |
| 474 | precomputedIntegrals->C(elIter,b,a,c)); |
| 475 | |
| 476 | // tensorProduct(qb,C); |
| 477 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[a], vertices[b], C, 2, phir, phis, result); |
| 478 | |
| 479 | memset(entry,0,sizeof(double)*27); |
| 480 | for(int j=0; j<3; j++) |
| 481 | for(int i=0; i<3; i++) |
| 482 | entry[TENSOR3_INDEX(j,i,j)] += C[i]; |
| 483 | |
| 484 | ADD_CONTRIB_ALL(c,a,b); |
| 485 | |
| 486 | //------- |
| 487 | |
| 488 | // <qa,C> I |
| 489 | //AddTensor3x3x3BlockContributionToQuadraticForm(vertices[c], vertices[b], vertices[a], C, 3, phir, phis, result); |
| 490 | |
| 491 | memset(entry,0,sizeof(double)*27); |
| 492 | for(int j=0; j<3; j++) |
| 493 | for(int i=0; i<3; i++) |
| 494 | entry[TENSOR3_INDEX(j,j,i)] += C[i]; |
| 495 | |
| 496 | ADD_CONTRIB_ALL(c,b,a); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | free(vertices); |
| 502 | |
| 503 | precomputedIntegrals->ReleaseElementIterator(elIter); |
| 504 | |
| 505 | if (verbose) |
| 506 | printf("\n"); |
| 507 | } |
| 508 | |
| 509 | void StVKHessianTensor::ComputeStiffnessMatrixCorrection(double * u, double * du, SparseMatrix * dK) |
| 510 | { |
| 511 | dK->ResetToZero(); |
| 512 | AddQuadraticTermsContribution(u, du, dK); |
| 513 | AddCubicTermsContribution(u, du, dK); |
| 514 | } |
| 515 | |
| 516 | #define ADD_MATRIX_BLOCK(where)\ |
| 517 | for(k=0; k<3; k++)\ |
| 518 | for(l=0; l<3; l++)\ |
| 519 | {\ |
| 520 | dataHandle[rowc+k][3*column[c8+(where)]+l] += matrix[3*k+l];\ |
| 521 | } |
| 522 | |
| 523 | void StVKHessianTensor::AddQuadraticTermsContribution(double * u, double * du, SparseMatrix * dK, int elementLow, int elementHigh) |
| 524 | { |
| 525 | if (elementLow < 0) |
| 526 | elementLow = 0; |
| 527 | if (elementHigh < 0) |
| 528 | elementHigh = volumetricMesh->getNumElements(); |
| 529 | |
| 530 | int ** row_, ** column_; |
| 531 | stVKStiffnessMatrix->GetMatrixAccelerationIndices(&row_, &column_); |
| 532 | |
| 533 | int * vertices = (int*) malloc (sizeof(int) * numElementVertices); |
| 534 | |
| 535 | void * elIter; |
| 536 | precomputedIntegrals->AllocateElementIterator(&elIter); |
| 537 | |
| 538 | double ** dataHandle = dK->GetDataHandle(); |
| 539 | |
| 540 | for(int el=elementLow; el < elementHigh; el++) |
| 541 | { |
| 542 | precomputedIntegrals->PrepareElement(el, elIter); |
| 543 | int * row = row_[el]; |
| 544 | int * column = column_[el]; |
| 545 | |
| 546 | for(int ver=0; ver<numElementVertices ;ver++) |
| 547 | vertices[ver] = volumetricMesh->getVertexIndex(el, ver); |
| 548 | |
| 549 | double lambda = lambdaLame[el]; |
| 550 | double mu = muLame[el]; |
| 551 | |
| 552 | for (int c=0; c<numElementVertices; c++) // over all vertices of the voxel |
| 553 | { |
| 554 | int rowc = 3*row[c]; |
| 555 | int c8 = numElementVertices*c; |
| 556 | for (int e=0; e<numElementVertices; e++) // compute contribution to block (c,e) of dK |
| 557 | { |
| 558 | double matrix[9]; |
| 559 | memset(matrix, 0, sizeof(double) * 9); |
| 560 | for(int f=0; f<numElementVertices; f++) |
| 561 | { |
| 562 | double qf[3] = { du[3*vertices[f]+0], du[3*vertices[f]+1], du[3*vertices[f]+2] }; |
| 563 | |
| 564 | Vec3d C0v = lambda * precomputedIntegrals->C(elIter,c,f,e) + mu * (precomputedIntegrals->C(elIter,e,f,c) + precomputedIntegrals->C(elIter,f,e,c)); |
| 565 | double C0[3] = {C0v[0], C0v[1], C0v[2]}; |
| 566 | |
| 567 | // C0 tensor qf |
| 568 | matrix[0] += C0[0] * qf[0]; matrix[1] += C0[0] * qf[1]; matrix[2] += C0[0] * qf[2]; |
| 569 | matrix[3] += C0[1] * qf[0]; matrix[4] += C0[1] * qf[1]; matrix[5] += C0[1] * qf[2]; |
| 570 | matrix[6] += C0[2] * qf[0]; matrix[7] += C0[2] * qf[1]; matrix[8] += C0[2] * qf[2]; |
| 571 | |
| 572 | Vec3d C1v = lambda * precomputedIntegrals->C(elIter,e,f,c) + mu * (precomputedIntegrals->C(elIter,c,e,f) + precomputedIntegrals->C(elIter,f,e,c)); |
| 573 | double C1[3] = {C1v[0], C1v[1], C1v[2]}; |
| 574 | |
| 575 | // qf tensor C1 |
| 576 | matrix[0] += qf[0] * C1[0]; matrix[1] += qf[0] * C1[1]; matrix[2] += qf[0] * C1[2]; |
| 577 | matrix[3] += qf[1] * C1[0]; matrix[4] += qf[1] * C1[1]; matrix[5] += qf[1] * C1[2]; |
| 578 | matrix[6] += qf[2] * C1[0]; matrix[7] += qf[2] * C1[1]; matrix[8] += qf[2] * C1[2]; |
| 579 | |
| 580 | Vec3d C2v = lambda * precomputedIntegrals->C(elIter,f,e,c) + mu * (precomputedIntegrals->C(elIter,c,f,e) + precomputedIntegrals->C(elIter,e,f,c)); |
| 581 | double C2[3] = {C2v[0], C2v[1], C2v[2]}; |
| 582 | |
| 583 | // qf dot C2 |
| 584 | double dotp = qf[0]*C2[0] + qf[1]*C2[1] + qf[2]*C2[2]; |
| 585 | matrix[0] += dotp; |
| 586 | matrix[4] += dotp; |
| 587 | matrix[8] += dotp; |
| 588 | } |
| 589 | int k,l; |
| 590 | ADD_MATRIX_BLOCK(e); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | precomputedIntegrals->ReleaseElementIterator(elIter); |
| 596 | |
| 597 | free(vertices); |
| 598 | } |
| 599 | |
| 600 | void StVKHessianTensor::AddCubicTermsContribution(double * u, double * du, SparseMatrix * dK, int elementLow, int elementHigh) |
| 601 | { |
| 602 | if (elementLow < 0) |
| 603 | elementLow = 0; |
| 604 | if (elementHigh < 0) |
| 605 | elementHigh = volumetricMesh->getNumElements(); |
| 606 | |
| 607 | int ** row_, ** column_; |
| 608 | stVKStiffnessMatrix->GetMatrixAccelerationIndices(&row_, &column_); |
| 609 | |
| 610 | int * vertices = (int*) malloc (sizeof(int) * numElementVertices); |
| 611 | |
| 612 | void * elIter; |
| 613 | precomputedIntegrals->AllocateElementIterator(&elIter); |
| 614 | |
| 615 | double ** dataHandle = dK->GetDataHandle(); |
| 616 | |
| 617 | for(int el=elementLow; el < elementHigh; el++) |
| 618 | { |
| 619 | precomputedIntegrals->PrepareElement(el, elIter); |
| 620 | int * row = row_[el]; |
| 621 | int * column = column_[el]; |
| 622 | |
| 623 | for(int ver=0; ver<numElementVertices ;ver++) |
| 624 | vertices[ver] = volumetricMesh->getVertexIndex(el, ver); |
| 625 | |
| 626 | double lambda = lambdaLame[el]; |
| 627 | double mu = muLame[el]; |
| 628 | |
| 629 | for (int c=0; c<numElementVertices; c++) // over all vertices of the voxel |
| 630 | { |
| 631 | int rowc = 3*row[c]; |
| 632 | int c8 = numElementVertices*c; |
| 633 | for (int e=0; e<numElementVertices; e++) // compute contribution to block (c,e) of dK |
| 634 | { |
| 635 | double matrix[9]; |
| 636 | memset(matrix, 0, sizeof(double) * 9); |
| 637 | for(int f=0; f<numElementVertices; f++) |
| 638 | { |
| 639 | double qf[3] = { du[3*vertices[f]+0], du[3*vertices[f]+1], du[3*vertices[f]+2] }; |
| 640 | for(int a=0; a<numElementVertices; a++) |
| 641 | { |
| 642 | double qa[3] = { u[3*vertices[a]+0], u[3*vertices[a]+1], u[3*vertices[a]+2] }; |
| 643 | |
| 644 | // qa tensor qf |
| 645 | double qaqf[9]; |
| 646 | qaqf[0] = qa[0] * qf[0]; qaqf[1] = qa[0] * qf[1]; qaqf[2] = qa[0] * qf[2]; |
| 647 | qaqf[3] = qa[1] * qf[0]; qaqf[4] = qa[1] * qf[1]; qaqf[5] = qa[1] * qf[2]; |
| 648 | qaqf[6] = qa[2] * qf[0]; qaqf[7] = qa[2] * qf[1]; qaqf[8] = qa[2] * qf[2]; |
| 649 | |
| 650 | double D0 = lambda * precomputedIntegrals->D(elIter,f,c,a,e) + mu * (precomputedIntegrals->D(elIter,f,e,a,c) + precomputedIntegrals->D(elIter,f,a,c,e)); |
| 651 | |
| 652 | matrix[0] += D0 * qaqf[0]; matrix[1] += D0 * qaqf[3]; matrix[2] += D0 * qaqf[6]; |
| 653 | matrix[3] += D0 * qaqf[1]; matrix[4] += D0 * qaqf[4]; matrix[5] += D0 * qaqf[7]; |
| 654 | matrix[6] += D0 * qaqf[2]; matrix[7] += D0 * qaqf[5]; matrix[8] += D0 * qaqf[8]; |
| 655 | |
| 656 | double D1 = lambda * precomputedIntegrals->D(elIter,a,c,f,e) + mu * (precomputedIntegrals->D(elIter,a,e,f,c) + precomputedIntegrals->D(elIter,a,f,c,e)); |
| 657 | |
| 658 | matrix[0] += D1 * qaqf[0]; matrix[1] += D1 * qaqf[1]; matrix[2] += D1 * qaqf[2]; |
| 659 | matrix[3] += D1 * qaqf[3]; matrix[4] += D1 * qaqf[4]; matrix[5] += D1 * qaqf[5]; |
| 660 | matrix[6] += D1 * qaqf[6]; matrix[7] += D1 * qaqf[7]; matrix[8] += D1 * qaqf[8]; |
| 661 | |
| 662 | double D2 = lambda * precomputedIntegrals->D(elIter,f,a,c,e) + mu * (precomputedIntegrals->D(elIter,f,c,a,e) + precomputedIntegrals->D(elIter,a,c,f,e)); |
| 663 | |
| 664 | // qf dot qa |
| 665 | double dotp = D2 * (qf[0]*qa[0] + qf[1]*qa[1] + qf[2]*qa[2]); |
| 666 | matrix[0] += dotp; |
| 667 | matrix[4] += dotp; |
| 668 | matrix[8] += dotp; |
| 669 | } |
| 670 | } |
| 671 | int k,l; |
| 672 | ADD_MATRIX_BLOCK(e); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | free(vertices); |
| 678 | |
| 679 | precomputedIntegrals->ReleaseElementIterator(elIter); |
| 680 | } |
| 681 | |
| 682 |