00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef SOFA_COMPONENT_FORCEFIELD_BEAMFEMFORCEFIELD_INL
00026 #define SOFA_COMPONENT_FORCEFIELD_BEAMFEMFORCEFIELD_INL
00027
00028 #include <sofa/core/componentmodel/behavior/ForceField.inl>
00029 #include <sofa/component/forcefield/BeamFEMForceField.h>
00030 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00031 #include <sofa/component/topology/GridTopology.h>
00032 #include <sofa/helper/PolarDecompose.h>
00033 #include <sofa/helper/gl/template.h>
00034 #include <sofa/helper/gl/Axis.h>
00035 #include <sofa/helper/rmath.h>
00036 #include <assert.h>
00037 #include <iostream>
00038 #include <set>
00039 #include <sofa/helper/system/gl.h>
00040 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00041 #include <sofa/defaulttype/VecTypes.h>
00042 #include <sofa/defaulttype/RigidTypes.h>
00043 #include <sofa/simulation/tree/GNode.h>
00044 using std::cerr;
00045 using std::endl;
00046 using std::set;
00047
00048
00049 namespace sofa
00050 {
00051
00052 namespace component
00053 {
00054
00055 namespace forcefield
00056 {
00057
00058 using namespace sofa::core;
00059 using namespace sofa::core::componentmodel;
00060 using namespace sofa::defaulttype;
00061
00062 template <class DataTypes>
00063 void BeamFEMForceField<DataTypes>::init()
00064 {
00065 this->core::componentmodel::behavior::ForceField<DataTypes>::init();
00066 sofa::core::objectmodel::BaseContext* context = this->getContext();
00067
00068 _topology = context->getMeshTopology();
00069
00070 stiffnessContainer = context->core::objectmodel::BaseContext::get<StiffnessContainer>();
00071 lengthContainer = context->core::objectmodel::BaseContext::get<LengthContainer>();
00072 poissonContainer = context->core::objectmodel::BaseContext::get<PoissonContainer>();
00073 radiusContainer = context->core::objectmodel::BaseContext::get<RadiusContainer>();
00074
00075 if (_topology==NULL)
00076 {
00077 std::cerr << "ERROR(BeamFEMForceField): object must have a BaseMeshTopology (i.e. EdgeSetTopology or MeshTopology).\n";
00078 return;
00079 }
00080 else
00081 {
00082 if(_topology->getNbEdges()==0)
00083 {
00084 std::cerr << "ERROR(BeamFEMForceField): topology is empty.\n";
00085 return;
00086 }
00087 _indexedElements = &_topology->getEdges();
00088 }
00089
00090 beamsData.setCreateFunction(BeamFEMEdgeCreationFunction);
00091 beamsData.setCreateParameter( (void *) this );
00092 beamsData.setDestroyParameter( (void *) this );
00093
00094 if (_initialPoints.getValue().size() == 0)
00095 {
00096 VecCoord& p = *this->mstate->getX();
00097 _initialPoints.setValue(p);
00098 }
00099
00100 reinit();
00101 }
00102
00103 template <class DataTypes>
00104 void BeamFEMForceField<DataTypes>::reinit()
00105 {
00106 unsigned int n = _indexedElements->size();
00107
00108
00109 _forces.resize( this->mstate->getSize() );
00110
00111 if (!stiffnessContainer)
00112 {
00114 std::cerr << "Warning(BeamFEMForceField): No object from StiffnessContainer has been loaded.\n";
00115 }
00116 if (!lengthContainer)
00117 {
00119 std::cerr << "Warning(BeamFEMForceField): No object from LengthContainer has been loaded.\n";
00120 }
00121 if (!poissonContainer)
00122 {
00124 std::cerr << "Warning(BeamFEMForceField): No object from PoissonContainer has been loaded.\n";
00125 }
00126 if (!radiusContainer)
00127 {
00129 std::cerr << "Warning(BeamFEMForceField): No object from RadiusContainer has been loaded.\n";
00130 }
00131
00132 initBeams( n );
00133 for (unsigned int i=0;i<n;++i)
00134 reinitBeam(i);
00135 std::cout << "BeamFEMForceField: init OK, "<<n<<" elements."<<std::endl;
00136 }
00137
00138 template <class DataTypes>
00139 void BeamFEMForceField<DataTypes>::reinitBeam(unsigned int i)
00140 {
00141 double stiffness, length, radius, poisson;
00142 Index a = (*_indexedElements)[i][0];
00143 Index b = (*_indexedElements)[i][1];
00144 const VecCoord& x0 = *this->mstate->getX0();
00145 std::cout << "Beam "<<i<<" : ("<<a<<' '<<b<<") : beamsData size = "<<beamsData.size()<<" mstate size = "<<this->mstate->getSize()<<" x0 size = "<<x0.size()<<std::endl;
00146
00147 if (stiffnessContainer)
00148 stiffness = stiffnessContainer->getStiffness(i) ;
00149 else stiffness = _youngModulus.getValue() ;
00150 if (lengthContainer)
00151 length = lengthContainer->getLength(i) ;
00152 else length = (x0[a].getCenter()-x0[b].getCenter()).norm() ;
00153 if (radiusContainer)
00154 radius = radiusContainer->getRadius(i) ;
00155 else radius = _radius.getValue() ;
00156 if (poissonContainer)
00157 poisson = poissonContainer->getPoisson(i) ;
00158 else poisson = _poissonRatio.getValue() ;
00159
00160 std::cout << i << " " << stiffness << " " << length << " " << poisson << " " << radius << std::endl;
00161 setBeam(i, stiffness, length, poisson, radius );
00162
00163 computeStiffness(i,a,b);
00164 initLarge(i,a,b);
00165 }
00166
00167 template<class DataTypes>
00168 void BeamFEMForceField<DataTypes>::BeamFEMEdgeCreationFunction(int edgeIndex, void* param, BeamInfo &ei,
00169 const topology::Edge& e, const sofa::helper::vector< unsigned int > &a,
00170 const sofa::helper::vector< double >&)
00171 {
00172 std::cout << "Create beam "<<edgeIndex<<" ("<<e<<") from "<<a<<std::endl;
00173 BeamFEMForceField<DataTypes>* p = static_cast<BeamFEMForceField<DataTypes>*>(param);
00174
00175 static_cast<BeamFEMForceField<DataTypes>*>(param)->reinitBeam(edgeIndex);
00176 ei = p->beamsData[edgeIndex];
00177
00178 }
00179
00180 template <class DataTypes>
00181 void BeamFEMForceField<DataTypes>::handleTopologyChange()
00182 {
00183
00184
00185 std::list<const sofa::core::componentmodel::topology::TopologyChange *>::const_iterator itBegin=_topology->firstChange();
00186 std::list<const sofa::core::componentmodel::topology::TopologyChange *>::const_iterator itEnd=_topology->lastChange();
00187
00188 beamsData.handleTopologyEvents(itBegin,itEnd);
00189 }
00190
00191 template<class DataTypes>
00192 void BeamFEMForceField<DataTypes>::addForce (VecDeriv& f, const VecCoord& p, const VecDeriv& )
00193 {
00194 f.resize(p.size());
00195
00196
00197
00198 unsigned int i;
00199
00200 _nodeRotations.resize(p.size());
00201
00202 for(i=0; i<p.size(); ++i)
00203 {
00204
00205
00206 p[i].getOrientation().toMatrix(_nodeRotations[i]);
00207 }
00208
00209 typename VecElement::const_iterator it;
00210
00211 for(it=_indexedElements->begin(),i=0;it!=_indexedElements->end();++it,++i)
00212 {
00213 Index a = (*it)[0];
00214 Index b = (*it)[1];
00215
00216 initLarge(i,a,b);
00217 accumulateForceLarge( f, p, i, a, b );
00218 }
00219
00220 }
00221
00222 template<class DataTypes>
00223 void BeamFEMForceField<DataTypes>::addDForce (VecDeriv& df, const VecDeriv& dx)
00224 {
00225 df.resize(dx.size());
00226
00227
00228 {
00229 unsigned int i=0;
00230 typename VecElement::const_iterator it;
00231
00232 for(it = _indexedElements->begin() ; it != _indexedElements->end() ; ++it, ++i)
00233 {
00234 Index a = (*it)[0];
00235 Index b = (*it)[1];
00236
00237 applyStiffnessLarge( df, dx, i, a, b );
00238 }
00239 }
00240 }
00241
00242 template<class DataTypes>
00243 typename BeamFEMForceField<DataTypes>::Real BeamFEMForceField<DataTypes>::peudo_determinant_for_coef ( const Mat<2, 3, Real>& M )
00244 {
00245 return M[0][1]*M[1][2] - M[1][1]*M[0][2] - M[0][0]*M[1][2] + M[1][0]*M[0][2] + M[0][0]*M[1][1] - M[1][0]*M[0][1];
00246 }
00247
00248 template<class DataTypes>
00249 void BeamFEMForceField<DataTypes>::computeStiffness(int i, Index , Index )
00250 {
00251 Real phiy, phiz;
00252 Real _L = (Real)beamsData[i]._L;
00253 Real _A = (Real)beamsData[i]._A;
00254 Real _nu = (Real)beamsData[i]._nu;
00255 Real _E = (Real)beamsData[i]._E;
00256 Real _Iy = (Real)beamsData[i]._Iy;
00257 Real _Iz = (Real)beamsData[i]._Iz;
00258 Real _Asy = (Real)beamsData[i]._Asy;
00259 Real _Asz = (Real)beamsData[i]._Asz;
00260 Real _G = (Real)beamsData[i]._G;
00261 Real _J = (Real)beamsData[i]._J;
00262 Real L2 = (Real) (_L * _L);
00263 Real L3 = (Real) (L2 * _L);
00264 Real EIy = (Real)(_E * _Iy);
00265 Real EIz = (Real)(_E * _Iz);
00266
00267
00268 if (_Asy == 0)
00269 phiy = 0.0;
00270 else
00271 phiy = (Real)(24.0*(1.0+_nu)*_Iz/(_Asy*L2));
00272
00273 if (_Asz == 0)
00274 phiz = 0.0;
00275 else
00276 phiz = (Real)(24.0*(1.0+_nu)*_Iy/(_Asz*L2));
00277
00278 StiffnessMatrix& k_loc = beamsData[i]._k_loc;
00279
00280
00281 k_loc.clear();
00282 k_loc[6][6] = k_loc[0][0] = _E*_A/_L;
00283 k_loc[7][7] = k_loc[1][1] = (Real)(12.0*EIz/(L3*(1.0+phiy)));
00284 k_loc[8][8] = k_loc[2][2] = (Real)(12.0*EIy/(L3*(1.0+phiz)));
00285 k_loc[9][9] = k_loc[3][3] = _G*_J/_L;
00286 k_loc[10][10] = k_loc[4][4] = (Real)((4.0+phiz)*EIy/(_L*(1.0+phiz)));
00287 k_loc[11][11] = k_loc[5][5] = (Real)((4.0+phiy)*EIz/(_L*(1.0+phiy)));
00288
00289 k_loc[4][2] = (Real)(-6.0*EIy/(L2*(1.0+phiz)));
00290 k_loc[5][1] = (Real)( 6.0*EIz/(L2*(1.0+phiy)));
00291 k_loc[6][0] = -k_loc[0][0];
00292 k_loc[7][1] = -k_loc[1][1];
00293 k_loc[7][5] = -k_loc[5][1];
00294 k_loc[8][2] = -k_loc[2][2];
00295 k_loc[8][4] = -k_loc[4][2];
00296 k_loc[9][3] = -k_loc[3][3];
00297 k_loc[10][2] = k_loc[4][2];
00298 k_loc[10][4] = (Real)((2.0-phiz)*EIy/(_L*(1.0+phiz)));
00299 k_loc[10][8] = -k_loc[4][2];
00300 k_loc[11][1] = k_loc[5][1];
00301 k_loc[11][5] = (Real)((2.0-phiy)*EIz/(_L*(1.0+phiy)));
00302 k_loc[11][7] = -k_loc[5][1];
00303
00304 for (int i=0; i<=10; i++)
00305 for (int j=i+1; j<12; j++)
00306 k_loc[i][j] = k_loc[j][i];
00307 }
00308
00309 inline Quat qDiff(Quat a, const Quat& b)
00310 {
00311 if (a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3]<0)
00312 {
00313 a[0] = -a[0];
00314 a[1] = -a[1];
00315 a[2] = -a[2];
00316 a[3] = -a[3];
00317 }
00318 Quat q = b.inverse() * a;
00319
00320 return q;
00321 }
00322
00324 template<class DataTypes>
00325 void BeamFEMForceField<DataTypes>::initLarge(int i, Index a, Index b)
00326 {
00327 const VecCoord& x = *this->mstate->getX();
00328
00329 Quat quatA, quatB, dQ;
00330 Vec3d dW;
00331
00332 quatA = x[a].getOrientation();
00333 quatB = x[b].getOrientation();
00334
00335
00336
00337 dQ = qDiff(quatB, quatA);
00338
00339 dW = dQ.toEulerVector();
00340
00341 double Theta = dW.norm();
00342
00343 if(Theta>0.0000001){
00344 dW.normalize();
00345
00346 beamQuat(i) = quatA*dQ.axisToQuat(dW, Theta/2);
00347 beamQuat(i).normalize();
00348 }
00349 else
00350 beamQuat(i)= quatA;
00351
00352 }
00353
00354 template<class DataTypes>
00355 void BeamFEMForceField<DataTypes>::accumulateForceLarge( VecDeriv& f, const VecCoord & x, int i, Index a, Index b )
00356 {
00357 const VecCoord& x0 = *this->mstate->getX0();
00358
00359 beamQuat(i)= x[a].getOrientation();
00360
00361 Vec<3,Real> u, P1P2, P1P2_0;
00362
00363 Displacement depl;
00364
00365
00366 P1P2_0 = x0[b].getCenter() - x0[a].getCenter();
00367 P1P2_0 = x0[a].getOrientation().inverseRotate(P1P2_0);
00368 P1P2 = x[b].getCenter() - x[a].getCenter();
00369 P1P2 = x[a].getOrientation().inverseRotate(P1P2);
00370 u = P1P2 - P1P2_0;
00371
00372 depl[0] = 0.0; depl[1] = 0.0; depl[2] = 0.0;
00373 depl[6] = u[0]; depl[7] = u[1]; depl[8] = u[2];
00374
00375
00376 Quat dQ0, dQ;
00377
00378
00379 dQ0 = qDiff(x0[b].getOrientation(), x0[a].getOrientation());
00380 dQ = qDiff(x[b].getOrientation(), x[a].getOrientation());
00381
00382 u = qDiff(dQ,dQ0).toEulerVector();
00383
00384 depl[3] = 0.0; depl[4] = 0.0; depl[5] = 0.0;
00385 depl[9] = u[0]; depl[10]= u[1]; depl[11]= u[2];
00386
00387
00388 Displacement force = beamsData[i]._k_loc * depl;
00389
00390
00391
00392
00393 Vec3d fa1 = x[a].getOrientation().rotate(Vec3d(force[0],force[1],force[2]));
00394 Vec3d fa2 = x[a].getOrientation().rotate(Vec3d(force[3],force[4],force[5]));
00395
00396 Vec3d fb1 = x[a].getOrientation().rotate(Vec3d(force[6],force[7],force[8]));
00397 Vec3d fb2 = x[a].getOrientation().rotate(Vec3d(force[9],force[10],force[11]));
00398
00399
00400 f[a] += Deriv(-fa1,-fa2);
00401 f[b] += Deriv(-fb1,-fb2);
00402
00403 }
00404
00405 template<class DataTypes>
00406 void BeamFEMForceField<DataTypes>::applyStiffnessLarge( VecDeriv& df, const VecDeriv& dx, int i, Index a, Index b )
00407 {
00408
00409
00410 Displacement local_depl;
00411 sofa::defaulttype::Vec<3,Real> u;
00412 const Quat& q = beamQuat(i);
00413
00414 u = q.inverseRotate(dx[a].getVCenter());
00415 local_depl[0] = u[0];
00416 local_depl[1] = u[1];
00417 local_depl[2] = u[2];
00418
00419 u = q.inverseRotate(dx[a].getVOrientation());
00420 local_depl[3] = u[0];
00421 local_depl[4] = u[1];
00422 local_depl[5] = u[2];
00423
00424
00425 u = q.inverseRotate(dx[b].getVCenter());
00426 local_depl[6] = u[0];
00427 local_depl[7] = u[1];
00428 local_depl[8] = u[2];
00429
00430 u = q.inverseRotate(dx[b].getVOrientation());
00431 local_depl[9] = u[0];
00432 local_depl[10] = u[1];
00433 local_depl[11] = u[2];
00434
00435 Displacement local_force = beamsData[i]._k_loc * local_depl;
00436
00437 Vec3d fa1 = q.rotate(Vec3d(local_force[0],local_force[1] ,local_force[2] ));
00438 Vec3d fa2 = q.rotate(Vec3d(local_force[3],local_force[4] ,local_force[5] ));
00439 Vec3d fb1 = q.rotate(Vec3d(local_force[6],local_force[7] ,local_force[8] ));
00440 Vec3d fb2 = q.rotate(Vec3d(local_force[9],local_force[10],local_force[11]));
00441
00442 df[a] += Deriv(-fa1,-fa2);
00443 df[b] += Deriv(-fb1,-fb2);
00444 }
00445
00446 template<class DataTypes>
00447 void BeamFEMForceField<DataTypes>::addKToMatrix(sofa::defaulttype::BaseMatrix *mat, SReal k, unsigned int &offset)
00448 {
00449
00450
00451 unsigned int i=0;
00452 typename VecElement::const_iterator it;
00453
00454 for(it = _indexedElements->begin() ; it != _indexedElements->end() ; ++it, ++i)
00455 {
00456 Index a = (*it)[0];
00457 Index b = (*it)[1];
00458
00459 Displacement local_depl;
00460 Vec3d u;
00461 const Quat& q = beamQuat(i);
00462 Transformation R,Rt;
00463 q.toMatrix(R);
00464 Rt.transpose(R);
00465 const StiffnessMatrix& K0 = beamsData[i]._k_loc;
00466 StiffnessMatrix K;
00467 for (int x1=0;x1<12;x1+=3)
00468 for (int y1=0;y1<12;y1+=3)
00469 {
00470 Mat<3,3,Real> m;
00471 K0.getsub(x1,y1, m);
00472 m = R*m*Rt;
00473 K.setsub(x1,y1, m);
00474 }
00475 int index[12];
00476 for (int x1=0;x1<6;x1++)
00477 index[x1] = offset+a*6+x1;
00478 for (int x1=0;x1<6;x1++)
00479 index[6+x1] = offset+b*6+x1;
00480 for (int x1=0;x1<12;++x1)
00481 for (int y1=0;y1<12;++y1)
00482 mat->add(index[x1], index[y1], - K(x1,y1)*k);
00483 }
00484 }
00485
00486 template<class DataTypes>
00487 void BeamFEMForceField<DataTypes>::draw()
00488 {
00489 if (!getContext()->getShowForceFields()) return;
00490 if (!this->mstate) return;
00491
00492 const VecCoord& x = *this->mstate->getX();
00493
00494
00495
00496 glDisable(GL_LIGHTING);
00497 glBegin(GL_LINES);
00498 typename VecElement::const_iterator it;
00499 int i;
00500 for(it = _indexedElements->begin(), i = 0 ; it != _indexedElements->end() ; ++it, ++i)
00501 {
00502 Index a = (*it)[0];
00503 Index b = (*it)[1];
00504
00505 defaulttype::Vec3d p; p = (x[a].getCenter()+x[b].getCenter())*0.5;
00506 Vec3d beamVec;
00507 beamVec[0]=beamsData[i]._L*0.5; beamVec[1] = 0.0; beamVec[2] = 0.0;
00508
00509 const Quat& q = beamQuat(i);
00510
00511 glColor3f(1,0,0);
00512 helper::gl::glVertexT(p - q.rotate(beamVec) );
00513 helper::gl::glVertexT(p + q.rotate(beamVec) );
00514
00515 beamVec[0]=0.0; beamVec[1] = beamsData[i]._L*0.5;
00516 glColor3f(0,1,0);
00517 helper::gl::glVertexT(p);
00518 helper::gl::glVertexT(p + q.rotate(beamVec) );
00519
00520 beamVec[1]=0.0; beamVec[2] = beamsData[i]._L*0.5;
00521 glColor3f(0,0,1);
00522 helper::gl::glVertexT(p);
00523 helper::gl::glVertexT(p + q.rotate(beamVec) );
00524 }
00525 glEnd();
00526 }
00527
00528 template<class DataTypes>
00529 void BeamFEMForceField<DataTypes>::initBeams(unsigned int size)
00530 {
00531 beamsData.resize(size);
00532 }
00533
00534 template<class DataTypes>
00535 void BeamFEMForceField<DataTypes>::setBeam(unsigned int i, double E, double L, double nu, double r)
00536 {
00537 beamsData[i].init(E,L,nu,r);
00538 }
00539
00540 template<class DataTypes>
00541 void BeamFEMForceField<DataTypes>::BeamInfo::init(double E, double L, double nu, double r)
00542 {
00543 _E = E;
00544 _E0 = E;
00545 _nu = nu;
00546 _L = L;
00547 _r = r;
00548
00549 _G=_E/(2.0*(1.0+_nu));
00550 _Iz = M_PI*r*r*r*r/4;
00551 _Iy = _Iz ;
00552 _J = _Iz+_Iy;
00553 _A = M_PI*r*r;
00554
00555 _Asy = 0.0;
00556 _Asz = 0.0;
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 }
00720
00721 }
00722
00723 }
00724
00725 #endif