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 "generateMassMatrix.h"
30 
computeMassMatrix(VolumetricMesh * volumetricMesh,SparseMatrix ** massMatrix,bool inflate3Dim)31 void GenerateMassMatrix::computeMassMatrix(
32   VolumetricMesh * volumetricMesh, SparseMatrix ** massMatrix, bool inflate3Dim)
33 {
34   int n = volumetricMesh->getNumVertices();
35   int numElementVertices = volumetricMesh->getNumElementVertices();
36   double * buffer = (double*) malloc (sizeof(double) * numElementVertices * numElementVertices);
37 
38   SparseMatrixOutline * massMatrixOutline;
39   if (!inflate3Dim)
40   {
41     massMatrixOutline = new SparseMatrixOutline(n);
42     for(int el=0; el <volumetricMesh->getNumElements(); el++)
43     {
44       volumetricMesh->computeElementMassMatrix(el, buffer);
45       for(int i=0; i < numElementVertices; i++)
46         for(int j=0; j < numElementVertices; j++)
47         {
48           massMatrixOutline->AddEntry(volumetricMesh->getVertexIndex(el,i),volumetricMesh->getVertexIndex(el,j),  buffer[numElementVertices * j + i]);
49         }
50     }
51   }
52   else
53   {
54     massMatrixOutline = new SparseMatrixOutline(3*n);
55     for(int el=0; el <volumetricMesh->getNumElements(); el++)
56     {
57       volumetricMesh->computeElementMassMatrix(el, buffer);
58       for(int i=0; i < numElementVertices; i++)
59         for(int j=0; j < numElementVertices; j++)
60         {
61           double entry = buffer[numElementVertices * j + i];
62           int indexi = volumetricMesh->getVertexIndex(el,i);
63           int indexj = volumetricMesh->getVertexIndex(el,j);
64           massMatrixOutline->AddEntry(3*indexi+0, 3*indexj+0, entry);
65           massMatrixOutline->AddEntry(3*indexi+1, 3*indexj+1, entry);
66           massMatrixOutline->AddEntry(3*indexi+2, 3*indexj+2, entry);
67         }
68     }
69   }
70 
71   (*massMatrix) = new SparseMatrix(massMatrixOutline);
72   delete(massMatrixOutline);
73 
74   free(buffer);
75 }
76 
computeVertexMasses(VolumetricMesh * volumetricMesh,double * masses,bool inflate3Dim)77 void GenerateMassMatrix::computeVertexMasses(VolumetricMesh * volumetricMesh, double * masses, bool inflate3Dim)
78 {
79   SparseMatrix * massMatrix;
80   computeMassMatrix(volumetricMesh, &massMatrix, inflate3Dim);
81   massMatrix->SumRowEntries(masses);
82   delete(massMatrix);
83 }
84 
85