Getting Started
In this introduction we will desribe the basic way of using cellular_raza
.
We assume that the reader is already somewhat familiar with the Rust programming language and has
installed the cargo package manager.
Simulation Code
To create a new project from scratch, initialize an empty project with cargo and change to this directory.
cargo init my-new-project
cd my-new-project
Afterwards add cellular_raza
as a dependency.
cargo add cellular_raza serde rand rand_chacha num
Afterwards, we have the following structure of files.
- main.rs
For now, we only implement physical interactions via the Mechanics and interaction simulation aspects. We can quickly build simulations by combining already existing building_blocks with the CellAgent derive macro.
Imports
We begin with some import statements which will be used later on.
|
|
Cellular Agent
In the next step, we define the cellular agent.
This simplistic example only considers two cellular aspects:
Mechanics
and Interaction
.
We describe the movement of cells via Langevin dynamics with the
Langevin2D
struct which
assumes that cells can be represented as point-like particles in $d=2$ dimensions.
Furthermore, every cell moves stochastically through space.
They interact via forces given by the
MorsePotential
struct.
|
|
We define a struct which stores all necessary parameters of the system. This gives us a unified way to change parameters of the simulation.
|
|
In the next step we initialize all components of the simulation.
We start by creating the cellular agents by using the values from the Parameters
struct.
Only the position is overwritten such that cells are placed inside the domain randomly.
We chose the CartesianCuboid
struct
as our domain and initialize it from the domain size and the interaction cutoff of the
MorsePotential
.
At last, we define start, end and time-increment together with the folder to store results.
This folder will be automatically created.
All properties are stored in the
Settings
struct.
|
|
Finally, we can run the simulation.
The chili backend uses the
run_simulation
macro to set up and
run the specified simulation.
Also, we need to tell our simulation which aspects to solve for.
|
|
Executing the Simulation
Now we can use cargo
to compile and execute the project in release mode with all possible
optimizations.
A progress bar will show up that indicates the progress and speed of execution.
$ cargo run -r
Finished `release` profile [optimized] target(s) in 0.40s
Running `my-new-project/target/release/cr_getting_started`
81%|██████████████████████▊ | 80856/100000 [00:02, 28803.93it/s]
During the simulation, results will be written to the out
folder which contains information about
every individual agent and the simulation domain.
In the next step, we will see how to visualize these results.
Visualization
Reading Results
To visualize the results spatially, we write a small python script.
We utilize the numpy
, matplotlib
and
tqdm
packages.
Every other functionality is contained in the standard library of python.
We again start by importing the required functionalities. Their uses will become clear in just a moment.
|
|
The results of the simulation have been saved in the popular json
format.
Cells and subdomains are stored in separate folders.
We define a couple of functions to load and store these results. Note that the only information related to the subdomains which is used for plotting is the total domain size which is fixed from the beginning. We can therefore load it only once and need not to change it again.
|
|
Creating Snapshots
It is usefull to define a new class for plotting the results.
The Plotter
class creates a figure and axis when being initialized.
It can reuse these variables when plotting multiple iterations in succession.
This is being done by the plot_iteration
method while save_iteration
also stores the created
figure in the path where the results are stored in.
|
|
Generating Movie (Optional)
We can create a movie from the individual snapshots.
To do this, we utilize ffmpeg
Code
|
|
Main
In our main function, we combine all the functionality defined above.
|
|
After we have generated the snapshots and the movie, our folder structure now contains these additional files.
-
-
-
-
- movie.mp4
-
-
-