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_GPU_CUDA_CUDASPRINGFORCEFIELD_H
00026 #define SOFA_GPU_CUDA_CUDASPRINGFORCEFIELD_H
00027
00028 #include "CudaTypes.h"
00029 #include <sofa/component/forcefield/SpringForceField.h>
00030 #include <sofa/component/forcefield/StiffSpringForceField.h>
00031 #include <sofa/component/forcefield/MeshSpringForceField.h>
00032
00033
00034 namespace sofa
00035 {
00036
00037 namespace gpu
00038 {
00039
00040 namespace cuda
00041 {
00042
00043 template<class DataTypes>
00044 class CudaKernelsSpringForceField;
00045
00046 }
00047
00048 }
00049
00050 namespace component
00051 {
00052
00053 namespace forcefield
00054 {
00055
00056 template <class TCoord, class TDeriv, class TReal>
00057 class SpringForceFieldInternalData< gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> >
00058 {
00059 public:
00060 typedef gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> DataTypes;
00061 typedef SpringForceField<DataTypes> Main;
00062 typedef typename Main::Inherit Inherit;
00063 typedef typename Main::Spring Spring;
00064 typedef SpringForceFieldInternalData<DataTypes> Data;
00065 typedef typename DataTypes::VecCoord VecCoord;
00066 typedef typename DataTypes::VecDeriv VecDeriv;
00067 typedef typename DataTypes::Coord Coord;
00068 typedef typename DataTypes::Deriv Deriv;
00069 typedef typename DataTypes::Real Real;
00070
00071 typedef gpu::cuda::CudaKernelsSpringForceField<DataTypes> Kernels;
00072
00073
00074 struct GPUSpring
00075 {
00076 int index;
00077
00078 float ks;
00079
00080 GPUSpring() : index(0), ks(0) {}
00081 void set(int index, float , float ks, float )
00082 {
00083 this->index = index+1;
00084
00085 this->ks = ks;
00086
00087 }
00088 };
00089 struct GPUSpring2
00090 {
00091
00092 float initpos;
00093
00094 float kd;
00095 GPUSpring2() : initpos(0), kd(0) {}
00096 void set(int , float initpos, float , float kd)
00097 {
00098
00099 this->initpos = initpos;
00100
00101 this->kd = kd;
00102 }
00103 };
00104 struct GPUSpringSet
00105 {
00106 int vertex0;
00107 int nbVertex;
00108 int nbSpringPerVertex;
00109 gpu::cuda::CudaVector<GPUSpring> springs;
00110 gpu::cuda::CudaVector<Real> dfdx;
00111 GPUSpringSet() : vertex0(0), nbVertex(0), nbSpringPerVertex(0) {}
00112 void init(int v0, int nbv, int nbsperv)
00113 {
00114 vertex0 = v0;
00115 nbVertex = nbv;
00116 nbSpringPerVertex = nbsperv;
00117 int nbloc = (nbVertex+BSIZE-1)/BSIZE;
00118 springs.resize(2*nbloc*nbSpringPerVertex*BSIZE);
00119 }
00120 void set(int vertex, int spring, int index, float initpos, float ks, float kd)
00121 {
00122 int bloc = vertex/BSIZE;
00123 int b_x = vertex%BSIZE;
00124 springs[ 2*bloc*BSIZE*nbSpringPerVertex
00125 + 2*spring*BSIZE
00126 + b_x
00127 ].set(index, initpos, ks, kd);
00128 (*(GPUSpring2*)&(springs[ 2*bloc*BSIZE*nbSpringPerVertex
00129 + 2*spring*BSIZE
00130 + b_x+BSIZE
00131 ])).set(index, initpos, ks, kd);
00132 }
00133 };
00134 GPUSpringSet springs1;
00135 GPUSpringSet springs2;
00136
00137 static void init(Main* m, bool stiff);
00138 static void addForce(Main* m, bool stiff, VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2);
00139 static void addDForce (Main* m, bool stiff, VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2, double kFactor, double bFactor);
00140 };
00141
00142
00143
00144
00145
00146
00147 #define CudaSpringForceField_DeclMethods(T) \
00148 template<> void SpringForceField< T >::init(); \
00149 template<> void SpringForceField< T >::addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2); \
00150 template<> void StiffSpringForceField< T >::init(); \
00151 template<> void StiffSpringForceField< T >::addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2); \
00152 template<> void StiffSpringForceField< T >::addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2, double kFactor, double bFactor);
00153
00154 CudaSpringForceField_DeclMethods(gpu::cuda::CudaVec3fTypes);
00155 CudaSpringForceField_DeclMethods(gpu::cuda::CudaVec3f1Types);
00156
00157
00158 #undef CudaSpringForceField_DeclMethods
00159
00160 }
00161
00162 }
00163
00164 }
00165
00166 #endif