{"id":626,"date":"2025-02-09T20:41:22","date_gmt":"2025-02-09T11:41:22","guid":{"rendered":"https:\/\/barbegenerativediary.com\/en\/?p=626"},"modified":"2025-06-20T10:16:19","modified_gmt":"2025-06-20T01:16:19","slug":"grid-based-partitioning-for-processing-optimizing-performance-in-large-simulations","status":"publish","type":"post","link":"https:\/\/barbegenerativediary.com\/en\/uncategorized\/grid-based-partitioning-for-processing-optimizing-performance-in-large-simulations\/","title":{"rendered":"Grid-Based Partitioning for Processing: Optimizing Performance in Large Simulations"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When handling a large number of objects, computational costs can become high, leading to decreased processing speed. This is especially true when performing collision detection or searching for nearby objects, where checking all combinations is inefficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we explore methods to significantly reduce computational load by utilizing spatial partitioning algorithms. Among various methods, we focus on the grid-based partitioning approach, which is relatively simple to implement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The full sample code for this article is available for download on Patreon after following.<\/strong><br>(It uses Processing for the implementation.)<br>\u2615 <strong>Support my Work:) \/  <a href=\"https:\/\/www.patreon.com\/barbe_generative_diary\/membership\" target=\"_blank\" rel=\"noopener\" title=\"\">Coffee Supplier on Patreon<\/a><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Spatial Partitioning?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Spatial partitioning is a technique that classifies objects into specific regions to reduce computational costs. Representative algorithms include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Grid-based Partitioning<\/strong>: Divides space into a grid and assigns objects to each cell.<\/li>\n\n\n\n<li><strong>Quadtree<\/strong>: A data structure that recursively subdivides 2D space into four parts.<\/li>\n\n\n\n<li><strong>Octree<\/strong>: A data structure that recursively subdivides 3D space into eight parts.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Grid-based Partitioning?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In grid-based partitioning, the simulation area is divided into cells of a specified size. Particles within each cell are managed in a list, and when calculating interactions between particles, only those in the target cell and its neighboring cells are considered. This approach reduces the number of calculations by focusing only on nearby particles, rather than all particles.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Determining Cell Size<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Deciding the cell size (cellSize) is one of the most crucial factors in spatial partitioning. If the cell size is too large, the calculation range becomes too broad, reducing efficiency. Conversely, if it&#8217;s too small, the number of cells increases, leading to higher management overhead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The cell size is determined based on MAX_DISTANCE (the maximum distance between particles). A suitable cellSize can be estimated using the following formula:<\/p>\n\n\n\n<pre><code class=\"language-processing\">\nint range = ceil(float(MAX_DISTANCE) \/ cellSize);\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>MAX_DISTANCE<\/code><\/th><th><code>cellSize<\/code><\/th><th><code>MAX_DISTANCE \/ cellSize<\/code><\/th><th><code>range<\/code> \u306e\u7406\u60f3\u5024<\/th><\/tr><\/thead><tbody><tr><td>100<\/td><td>50<\/td><td>2.0<\/td><td><code>ceil(2.0) = 2<\/code><\/td><\/tr><tr><td>100<\/td><td>100<\/td><td>1.0<\/td><td><code>ceil(1.0) = 1<\/code><\/td><\/tr><tr><td>100<\/td><td>200<\/td><td>0.5<\/td><td><code>ceil(0.5) = 1<\/code><\/td><\/tr><tr><td>150<\/td><td>50<\/td><td>3.0<\/td><td><code>ceil(3.0) = 3<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Grid Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The grid is typically implemented as a two-dimensional array. The size of the array is determined by dividing the width and height of the simulation space by cellSize. Each cell contains a list of particles present in the corresponding area.<\/p>\n\n\n\n<pre><code class=\"language-processing\">\n\/\/ Creating the grid\ncols = width \/ cellSize;\nrows = height \/ cellSize;\ngrid = new ArrayList[rows][cols];\n\n\/\/ Adding particles to cells\nfor (int i = 0; i < MAX_PARTICLES; i++) {\n   Particle p = new Particle();\n   grid[int(p.pos.y \/ cellSize)][int(p.pos.x \/ cellSize)].add(p);\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Neighbor Search<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The advantage of spatial partitioning becomes evident when calculating interactions between particles. Each particle searches only its own cell and the surrounding cells. The search range is determined based on the previously set range. If range is 1, the particle examines its own cell and the surrounding eight cells.<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, by using range, the particle searches its own cell and neighboring cells, interacting only with nearby particles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Advantages and Disadvantages<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reduction in Computation<\/strong>: By focusing only on particles in nearby cells instead of comparing all particles, computational load is significantly reduced.<\/li>\n\n\n\n<li><strong>Scalability<\/strong>: Even as the number of particles increases, the computational load does not increase drastically.<\/li>\n\n\n\n<li><strong>Efficient Memory Management<\/strong>: Managing particles by cell improves memory utilization efficiency.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Disadvantages<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overhead in Cell Management<\/strong>: There is a certain overhead in managing cells, especially when the cell size is small or the simulation space is large.<\/li>\n\n\n\n<li><strong>Balance in Spatial Partitioning<\/strong>: If the cell size is not set appropriately, the method may not function efficiently.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. Optimization Points<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Optimizing Cell Size<\/strong>: By appropriately setting cellSize based on MAX_DISTANCE, computational load can be minimized.<\/li>\n\n\n\n<li><strong>Adjusting Search Range<\/strong>: By adjusting the value of range, the number of cells to be calculated can be controlled, further reducing computational load.<\/li>\n\n\n\n<li><strong>Considering Parallelization<\/strong>: Since the calculation of interactions between particles is independent, utilizing parallel processing (e.g., leveraging the GPU) can achieve further speed improvements.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Below is a comparison between cases without and with spatial partitioning, showing the frame rate when running with 10,000 particles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Without Spatial Partitioning<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"576\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning1.webp\" alt=\"\" class=\"wp-image-628 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning1.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning1-300x169.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning1-768x432.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/576;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>With Spatial Partitioning<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"576\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning2.webp\" alt=\"\" class=\"wp-image-629 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning2.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning2-300x169.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/02\/250209-Grid-based-Partitioning2-768x432.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/576;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>Recommended Book<\/em><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"576\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/06\/Grid-Systems-Raster-Systeme.webp\" alt=\"Grid-Systems-Raster-Systeme\" class=\"wp-image-244 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/06\/Grid-Systems-Raster-Systeme.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/06\/Grid-Systems-Raster-Systeme-300x169.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/06\/Grid-Systems-Raster-Systeme-768x432.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/576;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/amzn.to\/3EBuSDS\">\"Grid Systems in Graphic Design\" by Josef M\u00fcller-Brockmann<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This book discusses grid systems, a layout method that enhances design coherence and readability by arranging text and images based on continuous rows and columns. Originally conceived for print layouts, grid systems are now utilized in all design fields, including web design. Even when creating 2D graphics with tools like Processing or P5.js, there is much to learn from this system. First published in 1981.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83c\udf10 <strong>Support my Website<\/strong><br>By using our affiliate links, you\u2019re helping my content and allows me to keep creating valuable articles. I appreciate it so much:)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tips-and-techniques-BGDsound\">BGD_SOUNDS (barbe_generative_diary SOUNDS)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">At BGD_SOUNDS, we have started sharing and selling a collection of field recordings stocked for use in sound visualization experiments. Nearly all recordings are captured in 192kHz-32bit float quality and are royalty-free, allowing for unrestricted use. New releases are added regularly, so if you\u2019re looking for sound sources, consider following us to stay updated.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"731\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS.webp\" alt=\"\" class=\"wp-image-554 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS-300x214.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS-768x548.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/731;\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we explore methods to significantly reduce computational load by utilizing spatial partitioning algorithms.<\/p>\n","protected":false},"author":1,"featured_media":627,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,13,1],"tags":[],"class_list":["post-626","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-processing-tutorials","category-tutorials","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/comments?post=626"}],"version-history":[{"count":3,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts\/626\/revisions"}],"predecessor-version":[{"id":787,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts\/626\/revisions\/787"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/media\/627"}],"wp:attachment":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/media?parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/categories?post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/tags?post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}