1#------------------------------------------------------------------------------ 2# Name: pychrono example 3# Purpose: 4# 5# Author: Alessandro Tasora 6# 7# Created: 1/01/2019 8# Copyright: (c) ProjectChrono 2019 9# 10# 11# This file shows how to create and populate the ChParticleClones object. 12# Also, shows how to use POV ray for postprocessing, thanks to the 13# utility functions in the unit_POSTPROCESS of Chrono::Engine. 14# 15#------------------------------------------------------------------------------ 16 17 18import pychrono as chrono 19import pychrono.postprocess as postprocess 20 21# We will create two directories for saving some files, we need this: 22import os 23 24# The path to the Chrono data directory containing various assets (meshes, textures, data files) 25# is automatically set, relative to the default location of this demo. 26# If running from a different directory, you must change the path to the data directory with: 27#chrono.SetChronoDataPath('path/to/data') 28 29 30# --------------------------------------------------------------------- 31# 32# Create the simulation system and add items 33# 34 35# Create a physical system, 36my_system = chrono.ChSystemNSC() 37 38 39# Set the default margins for collision detection, this is epecially 40# important for very large or very small objects. 41chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.001) 42chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.001) 43 44 45 46# Create the set of the particle clones (many rigid bodies that 47# share the same mass and collision shape, so they are memory efficient 48# in case you want to simulate granular material) 49 50body_particles = chrono.ChParticlesClones() 51body_particles.SetMass(0.01); 52inertia = 2/5*(pow(0.005,2))*0.01; 53body_particles.SetInertiaXX(chrono.ChVectorD(inertia,inertia,inertia)); 54 55# Collision shape (shared by all particle clones) Must be defined BEFORE adding particles 56particle_material = chrono.ChMaterialSurfaceNSC() 57 58body_particles.GetCollisionModel().ClearModel() 59body_particles.GetCollisionModel().AddSphere(particle_material, 0.005) 60body_particles.GetCollisionModel().BuildModel() 61body_particles.SetCollide(True) 62 63# add particles 64for ix in range(0,5): 65 for iy in range(0,5): 66 for iz in range(0,3): 67 body_particles.AddParticle(chrono.ChCoordsysD(chrono.ChVectorD(ix/100,0.1+iy/100, iz/100))) 68 69# Visualization shape (shared by all particle clones) 70body_particles_shape = chrono.ChSphereShape() 71body_particles_shape.GetSphereGeometry().rad = 0.005 72body_particles.GetAssets().push_back(body_particles_shape) 73 74my_system.Add(body_particles) 75 76 77 78 79# Create the floor: a simple fixed rigid body with a collision shape 80# and a visualization shape 81 82body_floor = chrono.ChBody() 83body_floor.SetBodyFixed(True) 84 85# Collision shape 86floor_material = chrono.ChMaterialSurfaceNSC() 87 88body_floor.GetCollisionModel().ClearModel() 89body_floor.GetCollisionModel().AddBox(floor_material, 0.1, 0.02, 0.1) # hemi sizes 90body_floor.GetCollisionModel().BuildModel() 91body_floor.SetCollide(True) 92 93# Visualization shape 94body_floor_shape = chrono.ChBoxShape() 95body_floor_shape.GetBoxGeometry().Size = chrono.ChVectorD(0.1, 0.02, 0.1) 96body_floor_shape.SetColor(chrono.ChColor(0.5,0.5,0.5)) 97body_floor.GetAssets().push_back(body_floor_shape) 98 99my_system.Add(body_floor) 100 101 102 103# Create boxes that fall 104# This is just for fun. 105 106for ix in range(0,2): 107 for iz in range(0,4): 108 body_brick = chrono.ChBody() 109 body_brick.SetPos(chrono.ChVectorD(0.05+ix*0.021,0.04,0+iz*0.021)) 110 body_brick.SetMass(0.02); 111 inertia = 2/5*(pow(0.01,2))*0.02; 112 body_brick.SetInertiaXX(chrono.ChVectorD(inertia,inertia,inertia)); 113 114 # Collision shape 115 body_brick.GetCollisionModel().ClearModel() 116 body_brick.GetCollisionModel().AddBox(floor_material, 0.01, 0.01, 0.01) # hemi sizes 117 body_brick.GetCollisionModel().BuildModel() 118 body_brick.SetCollide(True) 119 120 # Visualization shape 121 body_brick_shape = chrono.ChBoxShape() 122 body_brick_shape.GetBoxGeometry().Size = chrono.ChVectorD(0.01, 0.01, 0.01) 123 body_brick.GetAssets().push_back(body_brick_shape) 124 125 my_system.Add(body_brick) 126 127 128 129# --------------------------------------------------------------------- 130# 131# Render a short animation by generating scripts 132# to be used with POV-Ray 133# 134 135pov_exporter = postprocess.ChPovRay(my_system) 136 137# Important: set where the template is (this path depends to where you execute this script, 138# ex.here we assume you run it from src/demo/python/postprocess/ ) 139pov_exporter.SetTemplateFile ("../../../../data/_template_POV.pov") 140 141# Set the path where it will save all .pov, .ini, .asset and .dat files, 142# this directory will be created if not existing. For example: 143pov_exporter.SetBasePath("povray_pychrono_generated") 144 145 146# Some settings for the POV rendering: 147pov_exporter.SetCamera(chrono.ChVectorD(0.2,0.3,0.5), chrono.ChVectorD(0,0,0), 35) 148pov_exporter.SetLight(chrono.ChVectorD(-2,2,-1), chrono.ChColor(1.1,1.2,1.2), True) 149pov_exporter.SetPictureSize(640,480) 150pov_exporter.SetAmbientLight(chrono.ChColor(2,2,2)) 151 152 # Add additional POV objects/lights/materials in the following way 153pov_exporter.SetCustomPOVcommandsScript( 154''' 155light_source{ <1,3,1.5> color rgb<1.1,1.1,1.1> } 156Grid(0.05,0.04, rgb<0.7,0.7,0.7>, rgbt<1,1,1,1>) 157''') 158 159 # Tell which physical items you want to render 160pov_exporter.AddAll() 161 162 # Tell that you want to render the contacts 163pov_exporter.SetShowContacts(True, 164 postprocess.ChPovRay.SYMBOL_VECTOR_SCALELENGTH, 165 0.2, # scale 166 0.0007, # width 167 0.1, # max size 168 True,0,0.5 ) # colormap on, blue at 0, red at 0.5 169 170 # 1) Create the two .pov and .ini files for POV-Ray (this must be done 171 # only once at the beginning of the simulation). 172pov_exporter.ExportScript() 173 174#my_system.SetSolverType(chrono.ChSolver.Type_PMINRES) 175my_system.SetSolverMaxIterations(50) 176 177 178 # Perform a short simulation 179while (my_system.GetChTime() < 0.7) : 180 181 my_system.DoStepDynamics(0.005) 182 183 print ('time=', my_system.GetChTime() ) 184 185 # 2) Create the incremental nnnn.dat and nnnn.pov files that will be load 186 # by the pov .ini script in POV-Ray (do this at each simulation timestep) 187 pov_exporter.ExportData() 188 189 190 191