<commit_before>// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All right reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Author: <NAME>
// =============================================================================
//
// ChronoParallel unit test to compare solutions from different narrowphase
// algorithms
// The global reference frame has Z up.
// All units SI (CGS, i.e., centimeter - gram - second)
//
// =============================================================================

#include "chrono_parallel/physics/ChSystemParallel.h"
#include "chrono_parallel/lcp/ChLcpSystemDescriptorParallel.h"

#include "chrono_utils/ChUtilsCreators.h"
#include "chrono_utils/ChUtilsGenerators.h"
#include "chrono_utils/ChUtilsInputOutput.h"

#include "unit_testing.h"

// Comment the following line to use parallel collision detection
//#define BULLET

// Control use of OpenGL run-time rendering
//#undef CHRONO_PARALLEL_HAS_OPENGL

#ifdef CHRONO_PARALLEL_HAS_OPENGL
#include "chrono_opengl/ChOpenGLWindow.h"
#endif

using namespace chrono;
using namespace chrono::collision;

using std::cout;
using std::flush;
using std::endl;

// -----------------------------------------------------------------------------
// Global problem definitions
// -----------------------------------------------------------------------------
// Tolerance for test
double test_tolerance = 1e-5;

// Save PovRay post-processing data?
bool write_povray_data = true;

// Load the bodies from a checkpoint file?
bool loadCheckPointFile = false;

// Simulation times
double time_settling_min = 0.1;
double time_settling_max = 1.0;

// Stopping criteria for settling (fraction of particle radius)
double settling_tol = 0.2;

// Solver settings
double time_step = 1e-3;
int max_iteration_normal = 0;
int max_iteration_sliding = 1000;
int max_iteration_spinning = 0;
float contact_recovery_speed = 10e30;
double tolerance = 1e-2;

// Simulation frame at which detailed timing information is printed
int timing_frame = -1;

// Gravitational acceleration [m/s^2]
double gravity = 9.81;

// Parameters for the mechanism
int Id_container = 0;  // body ID for the containing bin
int Id_ground = 1;     // body ID for the ground

double hdimX = 2.0 / 2;   // [m] bin half-length in x direction
double hdimY = 2.0 / 2;   // [m] bin half-depth in y direction
double hdimZ = 2.0 / 2;   // [m] bin half-height in z direction
double hthick = 0.1 / 2;  // [m] bin half-thickness of the walls
float mu_walls = 0.3f;

// Parameters for the granular material
int Id_g = 2;         // start body ID for particles
double r_g = 0.1;     // [m] radius of granular sphers
double rho_g = 1000;  // [kg/m^3] density of granules
float mu_g = 0.5f;

void CreateMechanismBodies(ChSystemParallel* system) {
  ChSharedPtr<ChMaterialSurface> mat_walls(new ChMaterialSurface);
  mat_walls->SetFriction(mu_walls);

  ChSharedPtr<ChBody> container(new ChBody(
#ifndef BULLET
      new ChCollisionModelParallel
#endif
      ));
  container->SetMaterialSurface(mat_walls);
  container->SetIdentifier(Id_container);
  container->SetBodyFixed(true);
  container->SetCollide(true);
  container->SetMass(10000.0);

  // Attach geometry of the containing bin
  container->GetCollisionModel()->ClearModel();
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hdimX, hdimY, hthick), ChVector<>(0, 0, -hthick));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hthick, hdimY, hdimZ), ChVector<>(-hdimX - hthick, 0, hdimZ));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hthick, hdimY, hdimZ), ChVector<>(hdimX + hthick, 0, hdimZ));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hdimX, hthick, hdimZ), ChVector<>(0, -hdimY - hthick, hdimZ));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hdimX, hthick, hdimZ), ChVector<>(0, hdimY + hthick, hdimZ));
  container->GetCollisionModel()->BuildModel();

  system->AddBody(container);

  ChSharedPtr<ChBody> ground(new ChBody(
#ifndef BULLET
      new ChCollisionModelParallel
#endif
      ));
  ground->SetMaterialSurface(mat_walls);
  ground->SetIdentifier(Id_ground);
  ground->SetBodyFixed(true);
  ground->SetCollide(true);
  ground->SetMass(1.0);

  system->AddBody(ground);
}

void CreateGranularMaterial(ChSystemParallel* sys) {
  // Common material
  ChSharedPtr<ChMaterialSurface> ballMat(new ChMaterialSurface);
  ballMat->SetFriction(0);

  // Create the falling balls
  int ballId = 0;
  double mass = 1;
  double radius = 0.15;
  ChVector<> inertia = (2.0 / 5.0) * mass * radius * radius * ChVector<>(1, 1, 1);
  srand(1);

  for (int ix = -2; ix < 3; ix++) {
    for (int iy = -2; iy < 3; iy++) {
      for (int iz = -2; iz < 3; iz++) {
        ChVector<> rnd(rand() % 1000 / 100000.0, rand() % 1000 / 100000.0, rand() % 1000 / 100000.0);
        ChVector<> pos(0.4 * ix, 0.4 * iy, 0.4 * iz + 1);

        ChSharedBodyPtr ball(new ChBody(new ChCollisionModelParallel));
        ball->SetMaterialSurface(ballMat);

        ball->SetIdentifier(ballId++);
        ball->SetMass(mass);
        ball->SetInertiaXX(inertia);
        ball->SetPos(pos + rnd);
        ball->SetRot(ChQuaternion<>(1, 0, 0, 0));
        ball->SetBodyFixed(false);
        ball->SetCollide(true);

        ball->GetCollisionModel()->ClearModel();
        utils::AddSphereGeometry(ball.get_ptr(), radius);
        ball->GetCollisionModel()->BuildModel();

        sys->AddBody(ball);
      }
    }
  }
}
// =============================================================================

void SetupSystem(ChSystemParallelDVI* msystem) {
  msystem->Set_G_acc(ChVector<>(0, 0, -gravity));

  msystem->GetSettings()->solver.tolerance = tolerance;
  msystem->GetSettings()->solver.solver_mode = SLIDING;
  msystem->GetSettings()->solver.max_iteration_normal = max_iteration_normal;
  msystem->GetSettings()->solver.max_iteration_sliding = max_iteration_sliding;
  msystem->GetSettings()->solver.max_iteration_spinning = max_iteration_spinning;
  msystem->GetSettings()->solver.alpha = 0;
  msystem->GetSettings()->solver.contact_recovery_speed = contact_recovery_speed;
  msystem->SetMaxPenetrationRecoverySpeed(contact_recovery_speed);
  msystem->ChangeSolverType(APGD);
  msystem->GetSettings()->collision.collision_envelope = 0;
  msystem->GetSettings()->collision.bins_per_axis = I3(10, 10, 10);

  // Create the mechanism bodies (all fixed).
  CreateMechanismBodies(msystem);

  // Create granular material.
  CreateGranularMaterial(msystem);
}
// Sync the positions and velocities of the rigid bodies
void Sync(ChSystemParallel* msystem_A, ChSystemParallel* msystem_B) {
  for (int i = 0; i < msystem_A->Get_bodylist()->size(); i++) {<commit_msg>Update the narrowphase test so that it runs faster<commit_after>// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All right reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Author: <NAME>
// =============================================================================
//
// ChronoParallel unit test to compare solutions from different narrowphase
// algorithms
// The global reference frame has Z up.
// All units SI (CGS, i.e., centimeter - gram - second)
//
// =============================================================================

#include "chrono_parallel/physics/ChSystemParallel.h"
#include "chrono_parallel/lcp/ChLcpSystemDescriptorParallel.h"

#include "chrono_utils/ChUtilsCreators.h"
#include "chrono_utils/ChUtilsGenerators.h"
#include "chrono_utils/ChUtilsInputOutput.h"

#include "unit_testing.h"

// Comment the following line to use parallel collision detection
//#define BULLET

// Control use of OpenGL run-time rendering
//#undef CHRONO_PARALLEL_HAS_OPENGL

#ifdef CHRONO_PARALLEL_HAS_OPENGL
#include "chrono_opengl/ChOpenGLWindow.h"
#endif

using namespace chrono;
using namespace chrono::collision;

using std::cout;
using std::flush;
using std::endl;

// -----------------------------------------------------------------------------
// Global problem definitions
// -----------------------------------------------------------------------------
// Tolerance for test
double test_tolerance = 1e-5;

// Save PovRay post-processing data?
bool write_povray_data = true;

// Load the bodies from a checkpoint file?
bool loadCheckPointFile = false;

// Simulation times
double time_settling_min = 0.1;
double time_settling_max = 1.0;

// Stopping criteria for settling (fraction of particle radius)
double settling_tol = 0.2;

// Solver settings
double time_step = 1e-3;
int max_iteration_normal = 0;
int max_iteration_sliding = 25;
int max_iteration_spinning = 0;
float contact_recovery_speed = 10e30;
double tolerance = 1e-2;

// Simulation frame at which detailed timing information is printed
int timing_frame = -1;

// Gravitational acceleration [m/s^2]
double gravity = 9.81;

// Parameters for the mechanism
int Id_container = 0;  // body ID for the containing bin
int Id_ground = 1;     // body ID for the ground

double hdimX = 2.0 / 2;   // [m] bin half-length in x direction
double hdimY = 2.0 / 2;   // [m] bin half-depth in y direction
double hdimZ = 2.0 / 2;   // [m] bin half-height in z direction
double hthick = 0.1 / 2;  // [m] bin half-thickness of the walls
float mu_walls = 0.3f;

// Parameters for the granular material
int Id_g = 2;         // start body ID for particles
double r_g = 0.1;     // [m] radius of granular sphers
double rho_g = 1000;  // [kg/m^3] density of granules
float mu_g = 0.5f;

void CreateMechanismBodies(ChSystemParallel* system) {
  ChSharedPtr<ChMaterialSurface> mat_walls(new ChMaterialSurface);
  mat_walls->SetFriction(mu_walls);

  ChSharedPtr<ChBody> container(new ChBody(
#ifndef BULLET
      new ChCollisionModelParallel
#endif
      ));
  container->SetMaterialSurface(mat_walls);
  container->SetIdentifier(Id_container);
  container->SetBodyFixed(true);
  container->SetCollide(true);
  container->SetMass(10000.0);

  // Attach geometry of the containing bin
  container->GetCollisionModel()->ClearModel();
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hdimX, hdimY, hthick), ChVector<>(0, 0, -hthick));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hthick, hdimY, hdimZ), ChVector<>(-hdimX - hthick, 0, hdimZ));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hthick, hdimY, hdimZ), ChVector<>(hdimX + hthick, 0, hdimZ));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hdimX, hthick, hdimZ), ChVector<>(0, -hdimY - hthick, hdimZ));
  utils::AddBoxGeometry(container.get_ptr(), ChVector<>(hdimX, hthick, hdimZ), ChVector<>(0, hdimY + hthick, hdimZ));
  container->GetCollisionModel()->BuildModel();

  system->AddBody(container);

  ChSharedPtr<ChBody> ground(new ChBody(
#ifndef BULLET
      new ChCollisionModelParallel
#endif
      ));
  ground->SetMaterialSurface(mat_walls);
  ground->SetIdentifier(Id_ground);
  ground->SetBodyFixed(true);
  ground->SetCollide(true);
  ground->SetMass(1.0);

  system->AddBody(ground);
}

void CreateGranularMaterial(ChSystemParallel* sys) {
  // Common material
  ChSharedPtr<ChMaterialSurface> ballMat(new ChMaterialSurface);
  ballMat->SetFriction(1.0);

  // Create the falling balls
  int ballId = 0;
  double mass = 1;
  double radius = 0.15;
  ChVector<> inertia = (2.0 / 5.0) * mass * radius * radius * ChVector<>(1, 1, 1);
  srand(1);

  for (int ix = -2; ix < 3; ix++) {
    for (int iy = -2; iy < 3; iy++) {
      for (int iz = -2; iz < 3; iz++) {
        ChVector<> rnd(rand() % 1000 / 100000.0, rand() % 1000 / 100000.0, rand() % 1000 / 100000.0);
        ChVector<> pos(0.4 * ix, 0.4 * iy, 0.4 * iz + 1);

        ChSharedBodyPtr ball(new ChBody(new ChCollisionModelParallel));
        ball->SetMaterialSurface(ballMat);

        ball->SetIdentifier(ballId++);
        ball->SetMass(mass);
        ball->SetInertiaXX(inertia);
        ball->SetPos(pos + rnd);
        ball->SetRot(ChQuaternion<>(1, 0, 0, 0));
        ball->SetBodyFixed(false);
        ball->SetCollide(true);

        ball->GetCollisionModel()->ClearModel();
        utils::AddSphereGeometry(ball.get_ptr(), radius);
        ball->GetCollisionModel()->BuildModel();

        sys->AddBody(ball);
      }
    }
  }
}
// =============================================================================

void SetupSystem(ChSystemParallelDVI* msystem) {
  msystem->Set_G_acc(ChVector<>(0, 0, -gravity));

  msystem->GetSettings()->solver.tolerance = tolerance;
  msystem->GetSettings()->solver.solver_mode = SLIDING;
  msystem->GetSettings()->solver.max_iteration_normal = max_iteration_normal;
  msystem->GetSettings()->solver.max_iteration_sliding = max_iteration_sliding;
  msystem->GetSettings()->solver.max_iteration_spinning = max_iteration_spinning;
  msystem->GetSettings()->solver.alpha = 0;
  msystem->GetSettings()->solver.contact_recovery_speed = contact_recovery_speed;
  msystem->SetMaxPenetrationRecoverySpeed(contact_recovery_speed);
  msystem->ChangeSolverType(APGD);
  msystem->GetSettings()->collision.collision_envelope = 0;
  msystem->GetSettings()->collision.bins_per_axis = I3(10, 10, 10);
  msystem->GetSettings()->min_threads = 1;
  msystem->GetSettings()->max_threads = 1;

  // Create the mechanism bodies (all fixed).
  CreateMechanismBodies(msystem);

  // Create granular material.
  CreateGranularMaterial(msystem);
}
// Sync the positions and velocities of the rigid bodies
void Sync(ChSystemParallel* msystem_A, ChSystemParallel* msystem_B) {
  for (int i = 0; i < msystem_A->Get_bodylist()->size(); i++) {
{'fraction_non_alphanumeric': 0.1993784871812981, 'fraction_numerical': 0.020834804717847304, 'mean_word_length': 3.9510489510489513, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 157, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '930270', 'n_tokens_mistral': 4712, 'n_tokens_neox': 4395, 'n_words': 1068}