![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsFEMMeshTetrahedra.cpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (12/15/2009) 00009 UPDATE (09/03/2010) 00010 ******************************************************************************/ 00011 #include "TAPsFEMMeshTetrahedra.hpp" 00012 // Using Inclusion Model (i.e. definitions are included in declarations) 00013 // (this name.cpp is included in name.hpp) 00014 // Each friend is defined directly inside its declaration. 00015 00016 BEGIN_NAMESPACE_TAPs__FEM 00017 //============================================================================= 00018 // Constructors 00019 //----------------------------------------------------------------------------- 00020 template <typename T> 00021 MeshTetrahedra<T>::MeshTetrahedra () 00022 { 00023 //std::cout << "START: Default Ctor of MeshTetrahedra<T> ------------------\n"; 00024 Init(); 00025 //std::cout << "END: Default Ctor of MeshTetrahedra<T> --------------------\n"; 00026 } 00027 //----------------------------------------------------------------------------- 00028 //template <typename T> 00029 //MeshTetrahedra<T>::MeshTetrahedra ( MeshTetrahedra<T> const &orig ) 00030 //{} 00031 //----------------------------------------------------------------------------- 00032 template <typename T> 00033 MeshTetrahedra<T>::~MeshTetrahedra () 00034 {} 00035 //----------------------------------------------------------------------------- 00036 template <typename T> 00037 std::string MeshTetrahedra<T>::StrInfo () const 00038 { 00039 std::stringstream ss; 00040 std::vector< Tetrahedron<T> >::const_iterator it = m_vElements.begin(); 00041 ss << "FEM::Tetrahedra Mesh<" << typeid(T).name() << ">:\n"; 00042 while ( it != m_vElements.end() ) { 00043 ss << *it; 00044 ++it; 00045 } 00046 return ss.str(); 00047 } 00048 //----------------------------------------------------------------------------- 00049 00050 //----------------------------------------------------------------------------- 00051 template <typename T> 00052 void MeshTetrahedra<T>::Init () 00053 { 00054 m_vDeformedMeshNodes.push_back( Vector3<T>(0,0,0) ); 00055 m_vDeformedMeshNodes.push_back( Vector3<T>(0,0,0) ); 00056 m_vDeformedMeshNodes.push_back( Vector3<T>(0,0,0) ); 00057 m_vDeformedMeshNodes.push_back( Vector3<T>(0,0,0) ); 00058 m_vUndeformedMeshNodes.push_back( Vector3<T>(0,0,1) ); 00059 m_vUndeformedMeshNodes.push_back( Vector3<T>(0,1,0) ); 00060 m_vUndeformedMeshNodes.push_back( Vector3<T>(1,0,0) ); 00061 m_vUndeformedMeshNodes.push_back( Vector3<T>(0,0,0) ); 00062 00063 m_vElements.push_back( Tetrahedron<T>( 00064 &(RetListOfDeformedMeshNodes()), 00065 &(RetListOfUndeformedMeshNodes()), 00066 1.0, // Young's modulus 00067 0.01, // Poisson's ratio 00068 0, 1, 2, 3 ) 00069 ); 00070 } 00071 //----------------------------------------------------------------------------- 00072 00073 //============================================================================= 00074 // Assignment Operator 00075 //----------------------------------------------------------------------------- 00076 //template <typename T> 00077 //MeshTetrahedra<T> & MeshTetrahedra<T>::operator= ( MeshTetrahedra<T> const &orig ) 00078 //{} 00079 //----------------------------------------------------------------------------- 00080 //============================================================================= 00081 00082 00083 //============================================================================= 00084 // Operations 00085 //----------------------------------------------------------------------------- 00086 //template <typename T> 00087 //void MeshTetrahedra<T>::ResetToDeformedState () 00088 //{ 00089 // std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00090 // while ( it != m_vElements.end() ) { 00091 // it->ResetToDeformedState(); 00092 // ++it; 00093 // } 00094 //} 00095 //----------------------------------------------------------------------------- 00096 template <typename T> 00097 void MeshTetrahedra<T>::ResetToUndeformedState () 00098 { 00099 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00100 while ( it != m_vElements.end() ) { 00101 it->ResetToUndeformedState(); 00102 ++it; 00103 } 00104 00105 #ifdef TAPs_SIM_DYNAMIC_SYSTEM 00106 ResetCurrentVelocities(); 00107 #endif//TAPs_SIM_DYNAMIC_SYSTEM 00108 } 00109 //----------------------------------------------------------------------------- 00110 template <typename T> 00111 void MeshTetrahedra<T>::AssembleStiffnessMatrix () 00112 { 00113 #ifdef TAPs_USE_SYMMETRIC_MATRIX 00114 Mstiff.SetDirectAccessors(); 00115 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00116 while( it != m_vElements.end() ) { 00117 for ( int n = 0; n < 4; ++n ) { 00118 for ( int m = 0; m < 4; ++m ) { 00119 if ( it->ids[n] <= it->ids[m] ) { 00120 Mstiff.DirectAccess( it->ids[n] )->UpdateAdd( it->ids[m], it->GetStiffnessMatrix( n, m ) ); 00121 } 00122 } 00123 } 00124 ++it; 00125 } 00126 #else //TAPs_USE_SYMMETRIC_MATRIX 00127 Mstiff.SetDirectAccessors(); 00128 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00129 while( it != m_vElements.end() ) { 00130 for ( int n = 0; n < 4; ++n ) { 00131 for ( int m = 0; m < 4; ++m ) { 00132 Mstiff.DirectAccess( it->ids[n] )->UpdateAdd( it->ids[m], it->GetStiffnessMatrix( n, m ) ); 00133 } 00134 } 00135 ++it; 00136 } 00137 #endif//TAPs_USE_SYMMETRIC_MATRIX 00138 //*/ 00139 00140 // DEBUG 00141 //std::cout << "TetFEM Mstiff: " << Mstiff << "\n"; 00142 } 00143 //----------------------------------------------------------------------------- 00144 template <typename T> 00145 void MeshTetrahedra<T>::UpdateStrainDisplacementMatrix () 00146 { 00147 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00148 while ( it != m_vElements.end() ) { 00149 it->UpdateStrainDisplacementMatrixElements(); 00150 ++it; 00151 } 00152 } 00153 //----------------------------------------------------------------------------- 00154 //============================================================================= 00155 00156 00157 00158 00159 //============================================================================= 00160 // Get/Set Fns 00161 //----------------------------------------------------------------------------- 00162 template <typename T> 00163 unsigned int MeshTetrahedra<T>::GetNumOfElements () const 00164 { 00165 return m_vElements.size(); 00166 } 00167 00168 template <typename T> 00169 Tetrahedron<T> const & MeshTetrahedra<T>::RefToElement ( unsigned int i ) const 00170 { 00171 assert( i < GetNumOfElements() ); 00172 return m_vElements[i]; 00173 } 00174 00175 template <typename T> 00176 Tetrahedron<T> & MeshTetrahedra<T>::RefToElement ( unsigned int i ) 00177 { 00178 assert( i < GetNumOfElements() ); 00179 return m_vElements[i]; 00180 } 00181 //----------------------------------------------------------------------------- 00182 //============================================================================= 00183 00184 00185 00186 00187 //============================================================================= 00188 // Simulation 00189 //----------------------------------------------------------------------------- 00190 template <typename T> 00191 void MeshTetrahedra<T>::AdvanceSimulationStatic () 00192 { 00193 /* 00194 // TEST DATA -- CHECK PASSED 00195 // For one tetrahedron 00196 // these forces 00197 m_Forces[0].SetXYZ( 90.1, 99.2, 40.0 ); 00198 m_Forces[1].SetXYZ( -2.9, 19.4, 17.2 ); 00199 m_Forces[2].SetXYZ( -29.4, -50.4, -12.0 ); 00200 m_Forces[3].SetXYZ( -57.8, -68.2, -45.2 ); 00201 // results in the displacements of node 0 to 3 as follows: 00202 // Vector<double,3>( 0.354976, 0.0602062, 0.100089 )' 00203 // Vector<double,3>( -0.0882922, -0.000343599, 0.0135531 )' 00204 // Vector<double,3>( -0.0649275, 0.0043777, -0.0704632 )' 00205 // Vector<double,3>( -0.201756, -0.0642403, -0.0431789 )' 00206 //*/ 00207 00208 /* 00209 // TEST DATA -- CHECK PASSED 00210 // For one tetrahedron, these forces 00211 m_Forces[0].SetXYZ( 6, 104, 30 ); 00212 m_Forces[1].SetXYZ( -18, 44, 18 ); 00213 m_Forces[2].SetXYZ( 12, -72, -12 ); 00214 m_Forces[3].SetXYZ( 0, -76, -36 ); 00215 // with the filters set to 00216 m_Filter[0].SetXYZ(0,0,0); 00217 m_Filter[1].SetXYZ(0,1,0); 00218 m_Filter[2].SetXYZ(0,0,0); 00219 m_Filter[3].SetXYZ(0,0,0); 00220 // results in the displacements of node 0 to 3 as follows: 00221 // Vector<double,3>( 0, 0, 0 ); 00222 // Vector<double,3>( 0, 1, 0 ); 00223 // Vector<double,3>( 0, 0, 0 ); 00224 // Vector<double,3>( 0, 0, 0 ); 00225 //*/ 00226 00227 /* 00228 // TEST -- Whatever the value of f_1 is the displacement will be very high (the system is blown up) 00229 T f_1 = 5.0f; 00230 m_Forces[0].SetXYZ( f_1, f_1, f_1 ); 00231 m_Forces[1].SetXYZ( f_1, f_1, f_1 ); 00232 m_Forces[2].SetXYZ( f_1, f_1, f_1 ); 00233 m_Forces[3].SetXYZ( f_1, f_1, f_1 ); 00234 //*/ 00235 00236 Mesh<T>::AdvanceSimulationStatic(); 00237 00238 /* 00239 // TEST 00240 static int limit = 1; 00241 for ( int i = 0; i < limit; ++i ) { 00242 std::vector< Vector3<T> > & defNodes = RetListOfDeformedMeshNodes(); 00243 T o = T(-0.00001); 00244 defNodes[0] += Vector3<T>( o, o, o ); 00245 } 00246 //*/ 00247 } 00248 00249 #ifdef TAPs_SIM_DYNAMIC_SYSTEM 00250 template <typename T> 00251 void MeshTetrahedra<T>::AdvanceSimulationDynamic () 00252 { 00253 Mesh<T>::AdvanceSimulationDynamic(); 00254 } 00255 #endif//TAPs_SIM_DYNAMIC_SYSTEM 00256 00257 00258 //------------------------------------------------------------------- 00259 template <typename T> 00260 void MeshTetrahedra<T>::AllocateDataForSimulation ( T mass ) 00261 { 00262 Mesh<T>::AllocateDataForSimulation( mass ); 00263 } 00264 00265 //----------------------------------------------------------------------------- 00266 //============================================================================= 00267 00268 00269 //============================================================================= 00270 // Checking 00271 //----------------------------------------------------------------------------- 00272 template <typename T> 00273 std::string MeshTetrahedra<T>::CheckVolumeOfEachElement () 00274 { 00275 unsigned int count = 0; 00276 std::stringstream ss; 00277 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00278 while ( it != m_vElements.end() ) { 00279 ss //<< "Element# " << count++ << " Deformed - Undeformed Volume: " 00280 << "#" << count++ << ": " << it->CalDeformedVolume() << " - " << it->CalUndeformedVolume() << "; "; 00281 ++it; 00282 } 00283 return ss.str(); 00284 } 00285 //----------------------------------------------------------------------------- 00286 //============================================================================= 00287 00288 00289 00290 00291 //============================================================================= 00292 // For interactions 00293 #ifdef TAPs_ADD_INTERACTIONS 00294 //----------------------------------------------------------------------------- 00295 template <typename T> 00296 void MeshTetrahedra<T>::ApplyPointLoad ( PointForce<T> const * const ptrPtForce ) 00297 { 00298 // The force stored in or referred by the PointFoce is 00299 // in FEM local coordinates, not in world coordinates. 00300 // Hence, do need for coordinate conversion. 00301 00302 // Add distributed node forces 00303 if ( !IsFixedNode( m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 0 ) ) ) { 00304 // RefToPosition is barycentric coordinates 00305 T weight = T(1) - ptrPtForce->RefToPosition()[0] - ptrPtForce->RefToPosition()[1] - ptrPtForce->RefToPosition()[2]; 00306 m_Forces[ m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 0 ) ] += weight * ptrPtForce->RefToForce(); 00307 } 00308 if ( !IsFixedNode( m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 1 ) ) ) { 00309 m_Forces[ m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 1 ) ] += ptrPtForce->RefToPosition()[0] * ptrPtForce->RefToForce(); 00310 } 00311 if ( !IsFixedNode( m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 2 ) ) ) { 00312 m_Forces[ m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 2 ) ] += ptrPtForce->RefToPosition()[1] * ptrPtForce->RefToForce(); 00313 } 00314 if ( !IsFixedNode( m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 3 ) ) ) { 00315 m_Forces[ m_vElements[ ptrPtForce->GetID() ].GetGlobalNodeNumberFromLocalNode( 3 ) ] += ptrPtForce->RefToPosition()[2] * ptrPtForce->RefToForce(); 00316 } 00317 } 00318 //----------------------------------------------------------------------------- 00319 #endif//TAPs_ADD_INTERACTIONS 00320 //============================================================================= 00321 00322 00323 00324 00325 //============================================================================= 00326 // OpenGL 00327 #if defined(__gl_h_) || defined(__GL_H__) 00328 //----------------------------------------------------------------------------- 00329 template <typename T> 00330 void MeshTetrahedra<T>::Draw () 00331 { 00332 if ( m_pTrx ) { 00333 glPushMatrix(); 00334 m_pTrx->TransformByOpenGLForDrawing(); 00335 } 00336 00337 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00338 glDisable( GL_LIGHTING ); 00339 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00340 while ( it != m_vElements.end() ) { 00341 it->Draw(); 00342 ++it; 00343 } 00344 glPopAttrib(); 00345 00346 if ( m_pTrx ) { 00347 glPopMatrix(); 00348 } 00349 } 00350 00351 //----------------------------------------------------------------------------- 00352 template <typename T> 00353 void MeshTetrahedra<T>::DrawUndeformedMesh () 00354 { 00355 if ( m_pTrx ) { 00356 glPushMatrix(); 00357 m_pTrx->TransformByOpenGLForDrawing(); 00358 } 00359 00360 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00361 glDisable( GL_LIGHTING ); 00362 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00363 while ( it != m_vElements.end() ) { 00364 it->DrawUndeformedMesh(); 00365 ++it; 00366 } 00367 glPopAttrib(); 00368 00369 if ( m_pTrx ) { 00370 glPopMatrix(); 00371 } 00372 } 00373 00374 //----------------------------------------------------------------------------- 00375 template <typename T> 00376 void MeshTetrahedra<T>::DrawDeformedMesh () 00377 { 00378 if ( m_pTrx ) { 00379 glPushMatrix(); 00380 m_pTrx->TransformByOpenGLForDrawing(); 00381 } 00382 00383 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00384 glDisable( GL_LIGHTING ); 00385 std::vector< Tetrahedron<T> >::iterator it = m_vElements.begin(); 00386 while ( it != m_vElements.end() ) { 00387 it->DrawDeformedMesh(); 00388 ++it; 00389 } 00390 glPopAttrib(); 00391 00392 if ( m_pTrx ) { 00393 glPopMatrix(); 00394 } 00395 } 00396 00397 //----------------------------------------------------------------------------- 00398 #ifdef TAPs_ADD_INTERACTIONS 00399 template <typename T> 00400 void MeshTetrahedra<T>::DrawInteractionForces () 00401 { 00402 if ( m_pTrx ) { 00403 glPushMatrix(); 00404 m_pTrx->TransformByOpenGLForDrawing(); 00405 } 00406 00407 // Point forces 00408 ForceListsPtr<T>::TypeDef_PointForces::const_iterator pf = m_ForceLists.PointForces.begin(); 00409 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00410 glLineWidth( 1 ); 00411 glDisable( GL_LIGHTING ); 00412 glBegin( GL_LINES ); 00413 while ( pf != m_ForceLists.PointForces.end() ) { 00414 Vector3<T> pos = m_vElements[ (**pf).GetID() ].ComputeUndeformedPositionBasedOnCoordinates( 00415 0, // dummy value 00416 (**pf).RefToPosition()[0], (**pf).RefToPosition()[1], (**pf).RefToPosition()[2] ); 00417 00418 //std::cout << *(m_vElements[ (**pf).GetID() ].DeformedNode( 6 )) << "\n"; 00419 //std::cout << "pos: " << pos << "\n"; 00420 //std::cout << "force: " << (**pf).RefToForce() << "\n"; 00421 00422 glColor3f( 0, 1, 0 ); 00423 glVertex3fv( pos.GetDataFloat() ); 00424 glColor3f( 1, 0, 0 ); 00425 glVertex3fv( ((**pf).RefToForce()+pos).GetDataFloat() ); 00426 ++pf; 00427 } 00428 glEnd(); 00429 glPopAttrib(); 00430 00431 // CD forces 00432 pf = m_ForceLists.CDForces.begin(); 00433 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00434 glLineWidth( 1 ); 00435 glDisable( GL_LIGHTING ); 00436 //glTranslatef( 0, 0, 3 ); 00437 glBegin( GL_LINES ); 00438 while ( pf != m_ForceLists.CDForces.end() ) { 00439 Vector3<T> pos = m_vElements[ (**pf).GetID() ].ComputeUndeformedPositionBasedOnCoordinates( 00440 0, // dummy value 00441 (**pf).RefToPosition()[0], (**pf).RefToPosition()[1], (**pf).RefToPosition()[2] ); 00442 00443 //std::cout << *(m_vElements[ (**pf).GetID() ].DeformedNode( 6 )) << "\n"; 00444 //std::cout << "pos: " << pos << "\n"; 00445 //std::cout << (**pf).RefToPosition() << "\n"; 00446 //std::cout << "force: " << (**pf).RefToForce() << "\n"; 00447 00448 glColor3f( 0, 1, 0 ); 00449 glVertex3fv( pos.GetDataFloat() ); 00450 glColor3f( 1, 0, 0 ); 00451 glVertex3fv( ((**pf).RefToForce()+pos).GetDataFloat() ); 00452 ++pf; 00453 } 00454 glEnd(); 00455 glPopAttrib(); 00456 00457 if ( m_pTrx ) { 00458 glPopMatrix(); 00459 } 00460 } 00461 #endif//TAPs_ADD_INTERACTIONS 00462 //----------------------------------------------------------------------------- 00463 #endif // OpenGL 00464 //============================================================================= 00465 00466 00467 00468 00469 //----------------------------------------------------------------------------- 00470 //============================================================================= 00471 END_NAMESPACE_TAPs__FEM 00472 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00473 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----