Help initialising MPM particles inside a 3D mesh

Hello Taichi Wizards,
I’m trying to write MPM code that would have at the beginning particles lying inside a mesh.To make things easier, I voxelise the mesh so that I actually have a 3D volume with 1s tagging the ‘inside’ of the mesh and 0s tagging the outside. My approach has been then to scan the volume and use the coordinates of the voxels as the initial coordinate of the particles. In practice, I have a first python function config_particles which fills a list X with the coordinates, and then call a kernel seed to configure the particles (like in the example code). It works, because I can do X.length to know how many particles I want to create.

Here comes the problem: Now I want to have a lot of particles, and my python function config_particles takes ages to run! (python doesn’t like looping…). I was hoping to translate the config_particles into a kernel. However, I can’t know how many particles I’m going to create before running the config_particles function… is there a way of making an array grow inside a kernel without knowing how many elements will be added? Is there another approach you would advice for initialising MPM with a mesh (or a binary volume of voxels?)

Thank you in advance!


I am by no means an expert on Taichi or MPM, but I could imagine two ways of doing what I think you want to do.

  1. Run a function which determines how many particles you need to insert, save that number, call ti.reset() and allocate the necessary storage for the particles.
  2. Use ti.pointer or ti.dynamic. However, both require an upper bound for how many particles should be inserted at most. Since one of my open questions is regarding pointers, I don’t exactly know how it allocates memory and if dynamic is just a wrapper around pointer, and if this could solve your problem.

Hope this helped

thank you @digitalillusions! my problem currently is that the function that determines the exact number of particles to add is very slow (runs in vanilla python). But maybe I could just come up with a quick approximation and reserve slightly more particles with ti.dynamic? I’ll try that :slight_smile:

No problem. What I meant was you could run a Taichi kernel to figure out how many particles you need, then reset the runtime so you can place new tensors and allocate the right amount of memory.

hey! there’s a full solution implemented in the taichi_elements repo!


1 Like