NetDEM v1.0
Loading...
Searching...
No Matches
04_friction_test_trimesh.cpp

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

#include "cork_wrapper.hpp"
#include "particle.hpp"
#include "utils_math.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
using namespace netdem;
using namespace std;
void SaveDataset(string filename, VecXT<VecXT<double>> &data);
void FrictionTestTrimesh(int potential_case) {
// load particle
sh.InitFromSTL("data/particle_template.stl");
sh.SetSize(1.0);
cout << "shape size: " << sh.GetSize() << endl;
Particle obj_p1 = Particle(&sh);
Particle obj_p2 = Particle(&sh);
cout << "particle created ... " << endl;
// try another particle configuration
obj_p1.SetRodrigues(0.3, 0, 1, 0);
obj_p2.SetRodrigues(1.2, 0, 1, 0);
obj_p2.SetPosition(0, 0, 0.95);
obj_p2.SetVelocity(0.5, 0, 0);
// solver settings
SolverSDFPP cnt_solver;
cnt_solver.potential_type = potential_case;
cnt_solver.use_equivalent_stiffness = false;
LinearSpring cnt_model(1, 1, 0.3, 0);
ContactPP *cnt = nullptr;
double timestep = 1.0e-4;
for (int i = 0; i < 1001; i++) {
// clear the particle force and moment
obj_p1.ClearForce();
obj_p1.ClearMoment();
obj_p2.ClearForce();
obj_p2.ClearMoment();
// translational moving
obj_p2.UpdateMotion(timestep);
// contact detection and resolution procedure
cnt_solver.Init(&obj_p2, &obj_p1);
if (cnt_solver.Detect()) {
if (cnt == nullptr) {
cnt = new ContactPP(&obj_p2, &obj_p1);
cnt->SetCollisionModel(&cnt_model);
cnt_solver.ResolveInit(cnt, timestep);
} else {
cnt_solver.ResolveUpdate(cnt, timestep);
}
cnt->EvaluateForces(timestep);
double pos_x = obj_p2.pos[0];
Vec3d cnt_force{0, 0, 0}, cnt_moment{0, 0, 0};
Vec3d dir_n{0, 0, 0}, dir_s{0, 0, 0};
for (auto &cnt_entry : cnt->collision_entries) {
cnt_force[0] += cnt_entry.cnt_forces.force[0];
cnt_force[1] += cnt_entry.cnt_forces.force[1];
cnt_force[2] += cnt_entry.cnt_forces.force[2];
dir_n[0] += cnt_entry.cnt_forces.force_n[0];
dir_n[1] += cnt_entry.cnt_forces.force_n[1];
dir_n[2] += cnt_entry.cnt_forces.force_n[2];
dir_s[0] += cnt_entry.cnt_forces.force_t[0];
dir_s[1] += cnt_entry.cnt_forces.force_t[1];
dir_s[2] += cnt_entry.cnt_forces.force_t[2];
}
Math::Normalize(&dir_n);
Math::Normalize(&dir_s);
cout << pos_x << ", " << cnt->collision_entries.size() << ", "
<< cnt_force << ", " << dir_n << ", " << dir_s << endl;
cnt_data.push_back({pos_x, double(cnt->collision_entries.size()),
cnt_force[0], cnt_force[1], cnt_force[2], dir_n[0],
dir_n[1], dir_n[2], dir_s[0], dir_s[1], dir_s[2]});
} else {
double pos_x = obj_p2.pos[0];
cnt_data.push_back({pos_x, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0});
}
}
string root_dir = "local/potential_models/friction_test/";
filesystem::create_directories(root_dir);
switch (potential_case) {
case SolverSDFPP::PotentialType::linear:
SaveDataset(root_dir + "trimesh_linear.txt", cnt_data);
break;
case SolverSDFPP::PotentialType::hertz:
SaveDataset(root_dir + "trimesh_hertz.txt", cnt_data);
break;
default:
SaveDataset(root_dir + "trimesh_hertz.txt", cnt_data);
break;
}
if (cnt != nullptr) {
delete cnt;
cnt = nullptr;
}
}
A class representing a contact between two particles.
Definition contact_pp.hpp:20
void SetCollisionModel(ContactModel *const cnt_model)
Set the collision model used to calculate collision forces between particles.
Definition contact_pp.cpp:22
void ApplyToParticle()
Apply the contact forces and moments to the particles.
Definition contact_pp.cpp:48
VecXT< CollisionEntry > collision_entries
A list of CollisionEntry objects representing the collision geometries used by the contact model.
Definition contact_pp.hpp:47
void EvaluateForces(double dt)
Calculate and apply the contact forces and moments between particles.
Definition contact_pp.cpp:30
Contact model that uses linear spring elements to evaluate contact forces and moments.
Definition model_linear_spring.hpp:16
Definition particle.hpp:26
virtual void ClearMoment()
Clear all moments applied to the particle.
Definition particle.cpp:180
virtual void SetRodrigues(double angle, double axis_x, double axis_y, double axis_z)
Sets the orientation of the particle using a Rodrigues rotation vector.
Definition particle.cpp:95
virtual void UpdateMotion(double timestep)
Update the motion of the particle based on the applied forces and moments over a time step.
Definition particle.cpp:245
virtual void SetPosition(double pos_x, double pos_y, double pos_z)
Sets the position of the particle.
Definition particle.cpp:83
virtual void SetVelocity(double v_x, double v_y, double v_z)
Sets the velocity of the particle.
Definition particle.cpp:112
Vec3d pos
The position of the particle.
Definition particle.hpp:103
virtual void ClearForce()
Clear all forces applied to the particle.
Definition particle.cpp:174
virtual void EnableSurfaceNodes()
Enable the use of surface nodes.
Definition shape.cpp:284
virtual double GetSize() const
Return shape size, which is defined as the diameter of equal-volume sphere.
Definition shape.cpp:116
Signed distance field-based contact solver.
Definition solver_sdf_pp.hpp:19
void ResolveUpdate(ContactPP *const cnt, double timestep) override
Updates the contact resolution for a contact point.
Definition solver_sdf_pp.cpp:191
int potential_type
Whether to solve both sides of the collision.
Definition solver_sdf_pp.hpp:31
bool use_equivalent_stiffness
Definition solver_sdf_pp.hpp:38
bool Detect() override
Detects collisions between particles.
Definition solver_sdf_pp.cpp:73
void ResolveInit(ContactPP *const cnt, double timestep) override
Initializes the contact resolution for a contact point.
Definition solver_sdf_pp.cpp:163
void Init(Particle *const p1, Particle *const p2) override
Initializes the collision solver with two particles.
Definition solver_sdf_pp.cpp:18
A class representing a spherical harmonics object.
Definition shape_spherical_harmonics.hpp:24
void InitFromSTL(std::string const &file)
Initialize the SphericalHarmonics object from an STL file.
void SetSize(double d) override
Set the size of the SphericalHarmonics object.
Definition shape_spherical_harmonics.cpp:175
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31
std::array< double, 3 > Vec3d
Definition utils_macros.hpp:18
dir_n
Definition json_serilization.hpp:19
dir_s
Definition json_serilization.hpp:19