← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-pkg/yade/git-trunk] Rev 3896: Remove duplicated code.

 

------------------------------------------------------------
revno: 3896
committer: Anton Gladky <gladky.anton@xxxxxxxxx>
timestamp: Wed 2014-04-09 16:03:16 +0200
message:
  Remove duplicated code.
modified:
  pkg/common/SPHEngine.cpp
  pkg/common/SPHEngine.hpp
  pkg/dem/ViscoelasticPM.cpp
  pkg/dem/ViscoelasticPM.hpp


--
lp:yade
https://code.launchpad.net/~yade-pkg/yade/git-trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-pkg/yade/git-trunk/+edit-subscription
=== modified file 'pkg/common/SPHEngine.cpp'
--- pkg/common/SPHEngine.cpp	2014-04-09 14:03:15 +0000
+++ pkg/common/SPHEngine.cpp	2014-04-09 14:03:16 +0000
@@ -22,21 +22,8 @@
   Real rho = 0;
   
   // Pointer to kernel function
-  Real (*kernelFunctionCurrent)(const double & r, const double & h);
-  if (KernFunctionDensity==Poly6) {
-    kernelFunctionCurrent = smoothkernelPoly6;
-  } else if (KernFunctionDensity==Spiky) {
-    kernelFunctionCurrent = smoothkernelSpiky;
-  } else if (KernFunctionDensity==Visco) {
-    kernelFunctionCurrent = smoothkernelVisco;
-  } else if (KernFunctionDensity==Lucy) {
-    kernelFunctionCurrent = smoothkernelLucy;
-  } else if (KernFunctionDensity==Monaghan) {
-    kernelFunctionCurrent = smoothkernelMonaghan;
-  } else {
-    throw runtime_error("Kernel types can only have the following types: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5.");
-  }
-  
+  KernelFunction kernelFunctionCurDensity = returnKernelFunction (KernFunctionDensity, KernFunctionDensity, Norm);
+
   for(Body::MapId2IntrT::iterator it=b->intrs.begin(),end=b->intrs.end(); it!=end; ++it) {
     const shared_ptr<Body> b2 = Body::byId((*it).first,scene);
     Sphere* s=dynamic_cast<Sphere*>(b->shape.get());
@@ -54,10 +41,10 @@
       const Real SmoothDist = -geom.penetrationDepth + phys.h;
      
       // [Mueller2003], (3)
-      rho += b2->state->mass*kernelFunctionCurrent(SmoothDist, phys.h);
+      rho += b2->state->mass*kernelFunctionCurDensity(SmoothDist, phys.h);
     }
     // [Mueller2003], (3), we need to consider the density of the current body (?)
-    rho += b->state->mass*smoothkernelPoly6(0.0, s->radius);
+    rho += b->state->mass*kernelFunctionCurDensity(0.0, s->radius);
   }
   b->rho = rho;
 }
@@ -195,6 +182,64 @@
   return ret;
 }
 
+KernelFunction returnKernelFunction(const int a, const int b, const typeKernFunctions typeF) {
+  if (a != b) {
+    throw runtime_error("Kernel types should be equal! KERNELFUNCDESCR");
+  }
+  if (a==Poly6) {
+    if (typeF==Norm) {
+      return smoothkernelPoly6;
+    } else if (typeF==Grad) {
+      return smoothkernelPoly6Grad;
+    } else if (typeF==Lapl) {
+      return smoothkernelPoly6Lapl;
+    } else {
+      throw runtime_error("Type of kernel function undefined! KERNELFUNCDESCR");
+    }
+  } else if (a==Spiky) {
+    if (typeF==Norm) {
+      return smoothkernelSpiky;
+    } else if (typeF==Grad) {
+      return smoothkernelSpikyGrad;
+    } else if (typeF==Lapl) {
+      return smoothkernelSpikyLapl;
+    } else {
+      throw runtime_error("Type of kernel function undefined! KERNELFUNCDESCR");
+    }
+  } else if (a==Visco) {
+    if (typeF==Norm) {
+      return smoothkernelVisco;
+    } else if (typeF==Grad) {
+      return smoothkernelViscoGrad;
+    } else if (typeF==Lapl) {
+      return smoothkernelViscoLapl;
+    } else {
+    }
+  } else if (a==Lucy) {
+    if (typeF==Norm) {
+      return smoothkernelLucy;
+    } else if (typeF==Grad) {
+      return smoothkernelLucyGrad;
+    } else if (typeF==Lapl) {
+      return smoothkernelLucyLapl;
+    } else {
+      throw runtime_error("Type of kernel function undefined! KERNELFUNCDESCR");
+    }
+  } else if (a==Monaghan) {
+    if (typeF==Norm) {
+      return smoothkernelMonaghan;
+    } else if (typeF==Grad) {
+      return smoothkernelMonaghanGrad;
+    } else if (typeF==Lapl) {
+      return smoothkernelMonaghanLapl;
+    } else {
+      throw runtime_error("Type of kernel function undefined! KERNELFUNCDESCR");
+    }
+  } else {
+    throw runtime_error("Type of kernel function undefined! KERNELFUNCDESCR!");
+  }
+}
+
 void computeForceSPH(shared_ptr<IGeom>& _geom, shared_ptr<IPhys>& _phys, Interaction* I, Vector3r & force) {
   
   const ScGeom& geom=*static_cast<ScGeom*>(_geom.get());

=== modified file 'pkg/common/SPHEngine.hpp'
--- pkg/common/SPHEngine.hpp	2014-04-09 14:03:15 +0000
+++ pkg/common/SPHEngine.hpp	2014-04-09 14:03:16 +0000
@@ -4,16 +4,21 @@
 #include<yade/core/PartialEngine.hpp>
 #include<yade/pkg/dem/ScGeom.hpp>
 
+typedef Real (* KernelFunction)(const double & r, const double & h);
+
 enum KernFunctions {Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5};
+#define KERNELFUNCDESCR The following kernel functions are available: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5.
+
+enum typeKernFunctions {Norm, Grad, Lapl};
 class SPHEngine: public PartialEngine{
   public:
     void calculateSPHRho(const shared_ptr<Body>& b);
     virtual void action();
-  YADE_CLASS_BASE_DOC_ATTRS(SPHEngine,PartialEngine,"Apply given torque (momentum) value at every subscribed particle, at every step.",
+  YADE_CLASS_BASE_DOC_ATTRS(SPHEngine,PartialEngine,"Apply given torque (momentum) value at every subscribed particle, at every step. ",
     ((int, mask,-1,, "Bitmask for SPH-particles."))
     ((Real,k,-1,,    "Gas constant for SPH-interactions (only for SPH-model). See Mueller [Mueller2003]_ .")) // [Mueller2003], (11)
     ((Real,rho0,-1,, "Rest density. See Mueller [Mueller2003]_ ."))                                           // [Mueller2003], (1)
-    ((KernFunctions,KernFunctionDensity, Poly6,, "Kernel function for density calculation (by default - Poly6). The following kernel functions are available: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5."))
+    ((int,KernFunctionDensity, Poly6,, "Kernel function for density calculation (by default - Poly6). KERNELFUNCDESCR"))
   );
 };
 REGISTER_SERIALIZABLE(SPHEngine);
@@ -33,6 +38,8 @@
 Real smoothkernelMonaghanGrad(const double & r, const double & h);     
 Real smoothkernelMonaghanLapl(const double & r, const double & h);
 
+KernelFunction returnKernelFunction(const int a, const int b, const typeKernFunctions typeF);
+
 void computeForceSPH(shared_ptr<IGeom>& _geom, shared_ptr<IPhys>& _phys, Interaction* I, Vector3r & force);
 #endif
 

=== modified file 'pkg/dem/ViscoelasticPM.cpp'
--- pkg/dem/ViscoelasticPM.cpp	2014-04-09 14:03:15 +0000
+++ pkg/dem/ViscoelasticPM.cpp	2014-04-09 14:03:16 +0000
@@ -236,39 +236,13 @@
 		phys->mRtype = mRtype1;
 	}
 #ifdef YADE_SPH
-		if (mat1->SPHmode and mat2->SPHmode)  {
-			phys->SPHmode=true;
-			phys->mu=(mat1->mu+mat2->mu)/2.0;
-		}
-	
-	if (mat1->KernFunctionPressure==Poly6 and mat2->KernFunctionPressure==Poly6) {
-		phys->kernelFunctionCurrentPressure = smoothkernelPoly6Grad;
-	} else if (mat1->KernFunctionPressure==Spiky and mat2->KernFunctionPressure==Spiky) {
-		phys->kernelFunctionCurrentPressure = smoothkernelSpikyGrad;
-	} else if (mat1->KernFunctionPressure==Visco and mat2->KernFunctionPressure==Visco) {
-		phys->kernelFunctionCurrentPressure = smoothkernelViscoGrad;
-	} else if (mat1->KernFunctionPressure==Lucy and mat2->KernFunctionPressure==Lucy) {
-		phys->kernelFunctionCurrentPressure = smoothkernelLucyGrad;
-	} else if (mat1->KernFunctionPressure==Monaghan and mat2->KernFunctionPressure==Monaghan) {
-		phys->kernelFunctionCurrentPressure = smoothkernelMonaghanGrad;
-	} else {
-		throw runtime_error("Kernel types can only have the following types: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5. And for all contacting materials they should be equal.");
-	}
-	
-	if (mat1->KernFunctionVisco==Poly6 and mat2->KernFunctionVisco==Poly6) {
-		phys->kernelFunctionCurrentVisco = smoothkernelPoly6Lapl;
-	} else if (mat1->KernFunctionVisco==Spiky and mat2->KernFunctionVisco==Spiky) {
-		phys->kernelFunctionCurrentVisco = smoothkernelSpikyLapl;
-	} else if (mat1->KernFunctionVisco==Visco and mat2->KernFunctionVisco==Visco) {
-		phys->kernelFunctionCurrentVisco = smoothkernelViscoLapl;
-	} else if (mat1->KernFunctionVisco==Lucy and mat2->KernFunctionVisco==Lucy) {
-		phys->kernelFunctionCurrentVisco = smoothkernelLucyLapl;
-	} else if (mat1->KernFunctionVisco==Monaghan and mat2->KernFunctionVisco==Monaghan) {
-		phys->kernelFunctionCurrentVisco = smoothkernelMonaghanLapl;
-	} else {
-		throw runtime_error("Kernel types can only have the following types: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5. And for all contacting materials they should be equal.");
-	}
-	
+	if (mat1->SPHmode and mat2->SPHmode)  {
+		phys->SPHmode=true;
+		phys->mu=(mat1->mu+mat2->mu)/2.0;
+	}
+	
+	phys->kernelFunctionCurrentPressure = returnKernelFunction (mat1->KernFunctionPressure, mat2->KernFunctionPressure, Grad);
+	phys->kernelFunctionCurrentVisco    = returnKernelFunction (mat1->KernFunctionVisco, mat2->KernFunctionVisco, Lapl);
 #endif
 }
 

=== modified file 'pkg/dem/ViscoelasticPM.hpp'
--- pkg/dem/ViscoelasticPM.hpp	2014-04-09 14:03:15 +0000
+++ pkg/dem/ViscoelasticPM.hpp	2014-04-09 14:03:16 +0000
@@ -35,8 +35,8 @@
 #ifdef YADE_SPH
 		((bool,SPHmode,false,,"True, if SPH-mode is enabled."))
 		((Real,mu,-1,, "Viscosity. See Mueller [Mueller2003]_ ."))                                              // [Mueller2003], (14)
-		((KernFunctions,KernFunctionPressure,Spiky,, "Kernel function for pressure calculation (by default - Spiky). The following kernel functions are available: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5."))
-		((KernFunctions,KernFunctionVisco,   Visco,, "Kernel function for viscosity calculation (by default - Visco). The following kernel functions are available: Poly6=1, Spiky=2, Visco=3, Lucy=4, Monaghan=5."))
+		((int,KernFunctionPressure,Spiky,, "Kernel function for pressure calculation (by default - Spiky). KERNELFUNCDESCR"))
+		((int,KernFunctionVisco,   Visco,, "Kernel function for viscosity calculation (by default - Visco). KERNELFUNCDESCR"))
 #endif
 		((unsigned int,mRtype,1,,"Rolling resistance type, see [Zhou1999536]_. mRtype=1 - equation (3) in [Zhou1999536]_; mRtype=2 - equation (4) in [Zhou1999536]_.")),
 		createIndex();
@@ -62,8 +62,8 @@
 		((unsigned int,mRtype,1,,"Rolling resistance type, see [Zhou1999536]_. mRtype=1 - equation (3) in [Zhou1999536]_; mRtype=2 - equation (4) in [Zhou1999536]_")),
 		createIndex();
 	)
-		Real (*kernelFunctionCurrentPressure)(const double & r, const double & h);
-		Real (*kernelFunctionCurrentVisco)(const double & r, const double & h);
+		KernelFunction kernelFunctionCurrentPressure;
+		KernelFunction kernelFunctionCurrentVisco;
 	REGISTER_CLASS_INDEX(ViscElPhys,FrictPhys);
 };
 REGISTER_SERIALIZABLE(ViscElPhys);