NetDEM v1.0
Loading...
Searching...
No Matches
81_golf_ball_impact.cpp

This is an example of how to use the netdem library.

#include "data_dumper.hpp"
#include "gen_pack.hpp"
#include "gravity.hpp"
#include "shape_sphere.hpp"
#include "simulation.hpp"
#include <filesystem>
#include <iostream>
#include <unordered_map>
using namespace netdem;
using namespace std;
void GolfBallImpact() {
string result_dir = "tmp/examples/deformable_particle/golf_ball/";
filesystem::create_directories(result_dir);
// golf ball
Sphere sphere(0.04);
auto stl_model = sphere.GetSTLModel(500);
TriMesh trimesh;
trimesh.InitFromSTL(stl_model);
p.mesh_res = 20;
p.SetShape(&trimesh);
p.SetDensity(1150);
// E = 107 MPa, nu = 0.45
p.fem_simulator.neo_k = 3.57e8 / 5;
p.fem_simulator.neo_mu = 3.69e7 / 5;
p.fem_simulator.timestep = 1.0e-8;
p.SetPosition(-0.05, 0, 0);
p.SetVelocity(-67, 0, 0);
// plane wall
Plane plane(-0.1, 0, 0, 1, 0, 0);
Wall w(&plane);
// contact model and solver
LinearSpring cnt_model = LinearSpring(1.0e8, 1.0e8, 0.0, 0.0);
SolverSDFPW cnt_solver;
cnt_solver.potential_type = 0;
cnt_solver.solve_two_sides = true;
double dem_timestep = 1.0e-7;
// simulation
p.SaveAsVTK(result_dir + "p_tetmesh_000.vtk");
p.SaveSurfaceAsVTK(result_dir + "p_trimesh_000.vtk");
for (int ti = 0; ti < 400; ti++) {
for (int j = 0; j < 50; j++) {
cnt_solver.Init(&p, &w);
if (cnt_solver.Detect()) {
ContactPW cnt = ContactPW(&p, &w);
cnt.SetCollisionModel(&cnt_model);
cnt_solver.ResolveInit(&cnt, dem_timestep);
cnt.EvaluateForces(dem_timestep);
}
p.UpdateMotion(dem_timestep);
}
char filename[128];
snprintf(filename, 128, "p_tetmesh_%03d.vtk", ti + 1);
p.SaveAsVTK(result_dir + filename);
snprintf(filename, 128, "p_trimesh_%03d.vtk", ti + 1);
p.SaveSurfaceAsVTK(result_dir + filename);
}
}
A class representing a contact between a particle and a wall.
Definition contact_pw.hpp:22
void SetCollisionModel(ContactModel *const cnt_model)
Set the collision model used to calculate collision forces between the particle and the wall.
Definition contact_pw.cpp:22
void EvaluateForces(double dt)
Calculate and apply the contact forces and moments between the particle and the wall.
Definition contact_pw.cpp:30
A class representing a deformable particle simulated using the Finite Element Method (FEM).
Definition deformable_particle.hpp:21
void SetVelocity(double v_x, double v_y, double v_z) override
Sets the velocity of the particle.
Definition deformable_particle.cpp:114
void SetPosition(double x, double y, double z) override
Sets the position of the particle.
Definition deformable_particle.cpp:82
void ClearForce() override
Clears all forces on the particle's mesh.
Definition deformable_particle.cpp:150
void ApplyContactForce(ContactPP const *cnt) override
Applies a contact force to the particle.
Definition deformable_particle.cpp:158
void UpdateMotion(double dt) override
Updates the motion of the particle.
Definition deformable_particle.cpp:183
void SetShape(Shape *s) override
Sets the shape of the particle.
Definition deformable_particle.cpp:21
void SetDensity(double dens) override
Sets the density of the particle.
Definition deformable_particle.cpp:71
int mesh_res
The resolution of the tetrahedral mesh.
Definition deformable_particle.hpp:33
void SaveAsVTK(std::string const &filename) override
Saves the particle as a VTK file.
Definition deformable_particle.cpp:269
void SaveSurfaceAsVTK(std::string const &filename)
Saves the surface of the particle as a VTK file.
Definition deformable_particle.cpp:228
FEMSimulator fem_simulator
The FEM simulator object used to simulate the particle.
Definition deformable_particle.hpp:30
double timestep
The time step used in the simulation.
Definition fem_simulator.hpp:36
double neo_k
The bulk modulus of the material being simulated.
Definition fem_simulator.hpp:21
double damp_coef
The damping coefficient used in the simulation.
Definition fem_simulator.hpp:30
double neo_mu
The shear modulus of the material being simulated.
Definition fem_simulator.hpp:24
Contact model that uses linear spring elements to evaluate contact forces and moments.
Definition model_linear_spring.hpp:16
A class for representing a plane with a center point and normal vector.
Definition shape_plane.hpp:22
A class used to solve collisions between a particle and a wall using a signed distance field.
Definition solver_sdf_pw.hpp:18
bool Detect() override
Detects if there is a collision between the particle and the wall.
Definition solver_sdf_pw.cpp:71
void ResolveInit(ContactPW *const cnt, double timestep) override
Resolves the collision based on the initial contact information and timestep.
Definition solver_sdf_pw.cpp:171
void Init(Particle *const p, Wall *const w) override
Initializes the SolverSDFPW instance with the given particles and walls.
Definition solver_sdf_pw.cpp:16
int potential_type
Whether to solve both sides of the collision.
Definition solver_sdf_pw.hpp:30
bool solve_two_sides
Definition solver_sdf_pw.hpp:33
A class representing a sphere.
Definition shape_sphere.hpp:17
STLModel GetSTLModel(int num_nodes=200) override
Generate an STL model for the Sphere object.
Definition shape_sphere.cpp:97
A class representing a triangular mesh in 3D space.
Definition shape_trimesh.hpp:23
void InitFromSTL(std::string const &file)
Initialize the TriMesh object from an STL file.
A class representing a wall object in a physics simulation.
Definition wall.hpp:32
Definition bond_entry.hpp:7