Group Abstract Group Abstract

Message Boards Message Boards

Making efficient direct simulation Monte Carlo code

Posted 1 day ago

Hi!

I have developed a Mathematica code to solve the Boltzmann equation governing the evolution of a physical system. It is based on the so-called Direct Simulation Monte Carlo approach: one starts with a huge number of particles and simulates their collisions. Specifically, I build a table with each row representing a particle (with, say 10^7 rows), split it into n >> 1 sub-tables ("cells"), and launch the interaction kernels for sub-tables in parallel to simulate the interactions within each cell, isolated. Each interaction may modify the row (elastic scatterings), add new rows (backward annihilation), or remove rows (annihilation).

I heavily used built-in compilation features, with CompilationTarget->"C". However, I have reached the limits of my expertise in tuning its performance and overall design. Some of the most annoying points are that at the beginning of each timestep, I have to shuffle the whole table (mimic particles' migration from cell to cell), i.e., use RandomSample[table], which takes 20% of all the computational time; also, the initialization of the parallel routine governing dynamics within sub-tables (I do this using ParallelMap[compiledcode[#],subtables] - it is much faster than ParallelTable[compiledcode[tab],{tab,subtables}]) takes 40% of all the time just because of redistributing the large table over kernels.

For some reason, I prefer the main environment to simulate the dynamics to be Mathematica. While it may be, in principle, possible to make a C++ library and load it using LibraryLink (potentially preserving the C++ performance), I am wondering if it may be possible to improve the code such that it is maximally efficient within Mathematica only, to maintain flexibility. The C++ version (not implemented in Mathematica, though) already exists, having a much faster shuffling of the table and distributing the sub-table over kernels.

I seek advice regarding these points, and would be grateful for any help. However, given the large code, it may be unsuitable to copy-paste pieces of code here. Is there an option for how to proceed?

Welcome to Wolfram Community!
Please provide your efforts in the form of the Wolfram Language code. This will make it easier for other members to help you. Check several methods available to include your code in the rules http://wolfr.am/READ-1ST

POSTED BY: EDITORIAL BOARD
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard