NetDEM v1.0
Loading...
Searching...
No Matches
02_energy_test.cpp

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

#include "data_dumper.hpp"
#include "gen_pack.hpp"
#include "gravity.hpp"
#include "simulation.hpp"
#include <filesystem>
#include <iostream>
#include <unordered_map>
using namespace netdem;
using namespace std;
void SaveDataset(string filename, VecXT<VecXT<double>> &data);
void EnergyTest(int potential_case, int shape_case, double timestep) {
Simulation *sim = new Simulation();
sim->enable_logging = false;
sim->domain_manager.SetBound(-0.6, -0.6, -0.6, 0.6, 0.6, 0.6);
sim->domain_manager.SetCellSpacing(0.3, 0.3, 0.3);
LinearSpring cnt_model = LinearSpring(1.0e7, 1.0e6, 0.3, 0.0);
sim->scene.InsertContactModel(&cnt_model);
sim->scene.SetCollisionModel(0, 0, cnt_model.label);
switch (shape_case) {
case 0:
// sphere case
sh.a_nm[0] = 0.1 * sqrt(Math::PI);
break;
case 1:
// trimesh case
sh.InitFromSTL("data/particle_template.stl");
sh.SetSize(0.1);
break;
default:
// sphere case
cout << "please specify 0 for sphere, 1 for trimesh" << endl;
abort();
break;
}
sim->scene.InsertShape(&sh);
VecXT<Particle> particle_list = PackGenerator::GetGridPack(
1, 1, 1, 0, 0, 0, 7, 7, 7, sim->scene.GetShapes());
sim->scene.InsertParticle(particle_list);
WallBoxPlane wall_box(1, 1, 1, 0, 0, 0);
wall_box.ImportToScene(&(sim->scene));
// manipulate the solver settings
potential_case;
false;
// modifiers
Gravity grav;
grav.Init(sim);
sim->modifier_manager.Insert(&grav);
DataDumper data_dumper;
data_dumper.Init(sim);
data_dumper.SetRootPath("local/potential_models/energy_test/out/");
data_dumper.SetSaveByCycles(100);
data_dumper.dump_mesh = true;
data_dumper.dump_wall_info = true;
data_dumper.dump_contact_info = true;
sim->modifier_manager.Insert(&data_dumper);
sim->modifier_manager.Enable(data_dumper.label);
data_dumper.SaveShapeInfoAsSTL();
pe_evaluator.Init(sim);
pe_evaluator.SetParticleFromScene();
auto ev = sim->modifier_manager.Insert(&pe_evaluator);
auto pe_evaluator_ptr = static_cast<ParticleEnergyEvaluator *>(ev);
sim->modifier_manager.Enable(pe_evaluator.label);
sim->dem_solver.timestep = timestep;
VecXT<VecXT<double>> energy_data;
for (int i = 0; i < 300; i++) {
sim->Run(0.01);
// two particle case
// energy_data.push_back(
// {pe_evaluator_ptr->particle_energy_list[0].total,
// pe_evaluator_ptr->particle_energy_list[0].translational,
// pe_evaluator_ptr->particle_energy_list[0].rotational,
// pe_evaluator_ptr->particle_energy_list[0].gravitational,
// pe_evaluator_ptr->particle_energy_list[1].total,
// pe_evaluator_ptr->particle_energy_list[1].translational,
// pe_evaluator_ptr->particle_energy_list[1].rotational,
// pe_evaluator_ptr->particle_energy_list[1].gravitational});
// multiple particle case
auto tmp_energy = pe_evaluator_ptr->GetEnergy();
energy_data.push_back({tmp_energy.total, tmp_energy.translational,
tmp_energy.rotational, tmp_energy.gravitational});
}
string root_dir = "local/potential_models/energy_test/";
filesystem::create_directories(root_dir);
switch (shape_case) {
case 0:
switch (potential_case) {
case SolverSDFPP::PotentialType::linear:
SaveDataset(root_dir + "sphere_linear.txt", energy_data);
break;
case SolverSDFPP::PotentialType::hertz:
SaveDataset(root_dir + "sphere_hertz.txt", energy_data);
break;
default:
SaveDataset(root_dir + "sphere_hertz.txt", energy_data);
break;
}
break;
case 1:
switch (potential_case) {
case SolverSDFPP::PotentialType::linear:
SaveDataset(root_dir + "trimesh_linear.txt", energy_data);
break;
case SolverSDFPP::PotentialType::hertz:
SaveDataset(root_dir + "trimesh_hertz.txt", energy_data);
break;
default:
SaveDataset(root_dir + "trimesh_hertz.txt", energy_data);
break;
}
break;
default:
break;
}
sh.SaveAsSTL(root_dir + "shape.stl");
delete sim;
}
std::string label
Definition contact_model.hpp:34
ContactSolverSettings settings
Definition contact_solver_factory.hpp:64
int sdf_potential_type
Definition contact_solver_factory.hpp:43
bool sdf_use_equivalent_stiffness
Definition contact_solver_factory.hpp:50
double timestep
Definition dem_solver.hpp:42
ContactSolverFactory contact_solver_factory
Definition dem_solver.hpp:50
A class used to dump particle data into vtk files. This is a post-modifier, which will be executed at...
Definition data_dumper.hpp:12
void Init(Simulation *sim) override
Initializes the DataDumper instance.
Definition data_dumper.cpp:24
bool dump_wall_info
A flag that determines whether to dump wall information.
Definition data_dumper.hpp:21
bool dump_contact_info
A flag that determines whether to dump contact information.
Definition data_dumper.hpp:25
void SaveShapeInfoAsSTL()
Saves shape information as an STL file.
Definition data_dumper.cpp:2245
bool dump_mesh
A flag that determines whether to dump mesh information.
Definition data_dumper.hpp:33
void SetSaveByCycles(double interval)
Sets the interval for saving data by cycles.
Definition data_dumper.cpp:39
void SetRootPath(std::string const &root_path)
Sets the root directory path for the output file.
Definition data_dumper.cpp:26
void SetBound(double bmin_x, double bmin_y, double bmin_z, double bmax_x, double bmax_y, double bmax_z)
Sets the lower and upper bounds of the domain.
Definition domain_manager.cpp:83
void SetCellSpacing(double s_x, double s_y, double s_z)
Sets the spacing between cells in each dimension.
Definition domain_manager.cpp:105
A class used to apply gravity to particles in a DEM simulation.
Definition gravity.hpp:10
void Init(Simulation *sim) override
Initializes the Gravity instance.
Definition gravity.cpp:14
Contact model that uses linear spring elements to evaluate contact forces and moments.
Definition model_linear_spring.hpp:16
std::string label
Definition modifier.hpp:20
virtual void Init(Simulation *sim)
Initializes the Modifier instance.
Definition modifier.cpp:11
Modifier * Insert(Modifier *e)
Inserts new modifier into the simulation.
Definition modifier_manager.cpp:17
void Enable(std::string const &label)
Enables a modifier in the simulation.
A class used to evaluate the energy of particles in a DEM simulation.
Definition particle_energy_evaluator.hpp:27
void SetParticleFromScene()
Sets the particles to all the particles in the scene scene.
Definition particle_group.cpp:75
ContactModel * InsertContactModel(const ContactModel *const cm_ptr)
Insert a contact model into this scene.
Definition scene.cpp:290
void SetNumberOfMaterials(int num)
Set the number of materials in this scene and initialize the contact lookup table accordingly.
Definition scene.cpp:346
Shape * InsertShape(const Shape *const s_ptr)
Insert a single shape into this scene.
Definition scene.cpp:19
void SetCollisionModel(int mat_type_1, int mat_type_2, ContactModel *const cnt_model)
Set the collision model between two materials.
Definition scene.cpp:397
Particle * InsertParticle(const Particle *const p_ptr)
Insert a single particle into this scene.
Definition scene.cpp:76
VecXT< Shape * > GetShapes()
Return a vector of pointers to all shapes in this scene.
Definition scene.cpp:316
virtual void SaveAsSTL(std::string const &filename)
Saves the shape instance as an STL file.
Definition shape.cpp:353
Class for managing a DEM simulation.
Definition simulation.hpp:21
DEMSolver dem_solver
Implements DEM algorithms to solve the scene.
Definition simulation.hpp:47
void Run(double time)
Runs the simulation for a specified period of time.
Definition simulation.cpp:28
DomainManager domain_manager
Manager for domain and sub-domain calculations.
Definition simulation.hpp:31
ModifierManager modifier_manager
Manages add-on features (i.e., customized evaluations not hard-coded in the DEM calculation cycle).
Definition simulation.hpp:53
bool enable_logging
If true, output log information onto screen.
Definition simulation.hpp:69
Scene scene
Contains and manages basic DEM objects (e.g., shapes, particles, walls) for a DEM simulation.
Definition simulation.hpp:42
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
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
A class for generating a box of six walls.
Definition gen_wall_box_plane.hpp:23
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31