NetDEM v1.0
Loading...
Searching...
No Matches
00_contact_test_sphere.cpp

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

#include "igl_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) {
stringbuf buf;
ostream os(&buf);
int os_width = 24;
os.setf(ios::scientific);
os.precision(15);
for (auto &data_row : data) {
for (int j = 0; j < data_row.size() - 1; j++) {
os.width(os_width);
os << data_row[j] << ", ";
}
os.width(os_width);
os << data_row.back() << endl;
}
ofstream outfile;
outfile.open(filename);
if (!outfile) {
cout << "cannot open or create file: " << filename << endl;
abort();
}
outfile << buf.str();
outfile.close();
cout << "data saved to: " << filename << endl;
}
void ContactTestSphere(int potential_case, int surface_node_num) {
// load particle
sh.a_nm[0] = sqrt(Math::PI);
sh.SetSurfaceNodeNum(surface_node_num);
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.4, 0, 1, 0);
obj_p2.SetRodrigues(0.6, 0, 1, 0);
// solver settings
SolverSDFPP cnt_solver;
cnt_solver.potential_type = potential_case;
cnt_solver.use_equivalent_stiffness = false;
double dt = 1.0e-4;
LinearSpring cnt_model(1, 1, 0, 0);
ContactPP *cnt = nullptr;
// gradually push particle 2 onto particle 1
for (int i = 0; i < 61; i++) {
// clear the particle force and moment
obj_p1.ClearForce();
obj_p1.ClearMoment();
obj_p2.ClearForce();
obj_p2.ClearMoment();
// push down particle 2
obj_p2.SetPosition(0, 0, 1.01 - i * 0.001);
// 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, dt);
} else {
cnt_solver.ResolveUpdate(cnt, dt);
}
cnt->EvaluateForces(dt);
double len_n = 1.0 - obj_p2.pos[2];
cout << len_n << ", " << cnt->collision_entries.size() << ", "
<< obj_p2.force << ", " << obj_p2.moment << endl;
cnt_data.push_back({len_n, double(cnt->collision_entries.size()),
obj_p2.force[0], obj_p2.force[1], obj_p2.force[2],
obj_p2.moment[0], obj_p2.moment[1],
obj_p2.moment[2]});
}
}
// gradually move particle 2 away particle 1
for (int i = 0; i < 61; i++) {
obj_p1.ClearForce();
obj_p1.ClearMoment();
obj_p2.ClearForce();
obj_p2.ClearMoment();
obj_p2.SetPosition(0, 0, 1.01 - 60 * 0.001 + i * 0.001);
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, dt);
} else {
cnt_solver.ResolveUpdate(cnt, dt);
}
cnt->EvaluateForces(dt);
double len_n = 1.0 - obj_p2.pos[2];
cout << len_n << ", " << cnt->collision_entries.size() << ", "
<< obj_p2.force << ", " << obj_p2.moment << endl;
cnt_data.push_back({len_n, double(cnt->collision_entries.size()),
obj_p2.force[0], obj_p2.force[1], obj_p2.force[2],
obj_p2.moment[0], obj_p2.moment[1],
obj_p2.moment[2]});
}
}
string root_dir = "local/potential_models/contact_test/";
filesystem::create_directories(root_dir);
switch (potential_case) {
case SolverSDFPP::PotentialType::linear:
SaveDataset(root_dir + "sphere_linear.txt", cnt_data);
break;
case SolverSDFPP::PotentialType::hertz:
SaveDataset(root_dir + "sphere_hertz.txt", cnt_data);
break;
default:
SaveDataset(root_dir + "sphere_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 SetPosition(double pos_x, double pos_y, double pos_z)
Sets the position of the particle.
Definition particle.cpp:83
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
Vec3d moment
The moment acting on the particle.
Definition particle.hpp:138
Vec3d force
The force acting on the particle.
Definition particle.hpp:133
virtual void EnableSurfaceNodes()
Enable the use of surface nodes.
Definition shape.cpp:284
virtual void SetSurfaceNodeNum(int num)
Set number of surface nodes of the shape.
Definition shape.cpp:298
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
VecXT< double > a_nm
Definition shape_spherical_harmonics.hpp:27
void UpdateShapeProperties() override
Update the shape properties of the SphericalHarmonics object.
Definition shape_spherical_harmonics.cpp:129
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31
len_n
Definition json_serilization.hpp:20