Skip to content

Commit

Permalink
Merge pull request #5 from globus-labs/minor-improvements
Browse files Browse the repository at this point in the history
Improve camera position, add terrain randomness, and separate examples images
  • Loading branch information
gpauloski authored Sep 6, 2024
2 parents 5b663e5 + 3d46136 commit 1119ccf
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
images/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ python run_single.py --num-balls 100
```

You should see a pop up window that looks like the following.
![](images/simulation.png)
![](static/simulation.png)

The parameters of the simulation are fully configurable.
Use `python run_single.py --help` to see all of the options.

For example, set `--width 50` to increase the size of the terrain.
This will also zoom the camera out so it may be difficult to see the balls; the ball size can be changed with `--ball-diameter`.

## Simulate at Scale

Next, we can run the same simulation at scale by generating `n` random initial ball positions and submitting one simulation per ball to Globus Compute.
Expand All @@ -67,14 +70,18 @@ python run_globus_compute.py --num-balls 1000 --endpoint <UUID>
> [!TIP]
> Passing `--process-pool 8` instead of `--endpoint <UUID>` will run the simulations across a pool of 8 local processes rather than via Globus Compute.

> [!WARNING]
> Increasing the width of the terrain or the resolution of the terrain will increase the size of the mesh map.
> Globus Compute has a 10MB payload limit which can be exceeded if the terrain is too large.

Because the simulations are executed in parallel on remote processes, the simulations are performed in a "headless" mode (i.e., the simulation is not rendered to the screen).
The script visualizes the results of the simulations via plots that are saved to `images/`.

**2D Contour Plot** (`images/contour.png`)
![](images/contour.png)
![](static/contour.png)

**3D Terrain Heatmap** (`images/terrain.png`)
![](images/terrain.png)
![](static/terrain.png)

## Contributing

Expand Down
Binary file removed images/simulation.png
Binary file not shown.
2 changes: 1 addition & 1 deletion run_globus_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def main(argv: Sequence[str] | None = None) -> int:
sim_config = SimulationConfig.from_args(args)
terrain_config = TerrainConfig.from_args(args)

terrain_heightmap = generate_noisemap(terrain_config)
terrain_heightmap = generate_noisemap(terrain_config, seed=sim_config.seed)
terrain_mesh = generate_vertices(terrain_heightmap, terrain_config)

initial_positions = generate_initial_positions(
Expand Down
2 changes: 1 addition & 1 deletion run_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def main(argv: Sequence[str] | None = None) -> int:
sim_config = SimulationConfig.from_args(args)
terrain_config = TerrainConfig.from_args(args)

terrain_heightmap = generate_noisemap(terrain_config)
terrain_heightmap = generate_noisemap(terrain_config, seed=sim_config.seed)
terrain_mesh = generate_vertices(terrain_heightmap, terrain_config)

initial_positions = generate_initial_positions(
Expand Down
6 changes: 6 additions & 0 deletions simulation/plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pathlib

import matplotlib.pyplot as plt
import numpy as np
from numpy.typing import NDArray
Expand Down Expand Up @@ -42,6 +44,8 @@ def create_contour_plot(
ax.set_facecolor('#58a177')

fig.tight_layout(w_pad=2)

pathlib.Path(filepath).parent.mkdir(parents=True, exist_ok=True)
fig.savefig(filepath, pad_inches=0.05, dpi=300)


Expand Down Expand Up @@ -101,4 +105,6 @@ def create_terrain_plot(

fig.tight_layout()
fig.subplots_adjust(wspace=0.15, left=0, right=0.92, bottom=0.05, top=0.98)

pathlib.Path(filepath).parent.mkdir(parents=True, exist_ok=True)
fig.savefig(filepath, dpi=300)
15 changes: 11 additions & 4 deletions simulation/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,22 @@ def _generate() -> Position:
return [_generate() for _ in range(num_balls)]


def generate_noisemap(config: TerrainConfig) -> NDArray[np.float64]:
def generate_noisemap(
config: TerrainConfig,
seed: int | None = None,
) -> NDArray[np.float64]:
dimension = config.width * config.resolution
heightmap = np.zeros((dimension, dimension))

offset = seed if seed is not None else random.randint(0, 1_000_000)

for i in range(dimension):
for j in range(dimension):
x = (i / config.resolution) / config.scale
y = (j / config.resolution) / config.scale
heightmap[i][j] = pnoise2(
(i / config.resolution) / config.scale,
(j / config.resolution) / config.scale,
x + offset,
y + offset,
octaves=config.octaves,
persistence=config.persistence,
lacunarity=config.lacunarity,
Expand Down Expand Up @@ -231,7 +238,7 @@ def run_simulation(
if gui:
p.configureDebugVisualizer(p.COV_ENABLE_GUI, 0)
p.resetDebugVisualizerCamera(
cameraDistance=terrain_config.width / 2,
cameraDistance=max(terrain_config.width / 2, 10),
cameraYaw=0,
cameraPitch=-55,
cameraTargetPosition=[
Expand Down
File renamed without changes
Binary file added static/simulation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes

0 comments on commit 1119ccf

Please sign in to comment.