Introduction#

This is the simplest simulation you can run with JaxDEM.

import tempfile
from pathlib import Path
import jaxdem as jdem

Initialize the Simulation State#

First, we create a jaxdem.state.State object. It holds all per-particle data in the simulation. Here we create a single particle at the origin. jaxdem.state.State.create() fills every unspecified attribute with a default value.

state = jdem.State.create(pos=[[0.0, 0.0, 0.0]])

Note that we wrote pos=[[coords]], not pos=[coords]. jaxdem.state.State.create() expects a 2-D array of shape (N, dim), so even a single particle needs a list of lists.

Initialize the Simulation System#

Next, we create a jaxdem.system.System. It holds the global configuration of the simulation (domain, force model, integrator, …). Like the state, jaxdem.system.System.create() fills in defaults for anything we do not specify. The only requirement is that the system dimension matches the state dimension:

system = jdem.System.create(state.shape)

Run the Simulation#

Finally, we advance the simulation by calling jaxdem.system.System.step():

n_steps = 10
state, system = system.step(state, system, n=n_steps)

Saving the Simulation#

The last step is to save the simulation to VTK files so we can visualize it in ParaView. Writes happen asynchronously in background threads. Use the writer as a context manager (or call writer.block_until_ready() before exiting) to make sure the writer finishes all files:

tmp_dir = Path(tempfile.gettempdir()) / "data"
with jdem.VTKWriter(directory=tmp_dir) as writer:
    writer.save(state, system)

Where to Go Next#

Now that you have seen the simplest simulation, explore the rest of the user guide to learn about each component in depth:

Total running time of the script: (0 minutes 2.398 seconds)