1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! 🌶️ A modular, reusable, general-purpose backend
//!
//! # Usage
//!
//! In the following example, we provide an extremely short basic usage of the [run_simulation]
//! macro.
//! It performs the numerical integration of a well-defined problem.
//! We assume that the `MyAgent` struct and `MyDomain` have already been defined and implement the
//! [Mechanics](cellular_raza_concepts::Mechanics) concept.
//! More details about how to use pre-existing building blocks or derive their functionality is
//! provided by the
//! [cellular_raza_building_blocks](https://cellular-raza.com/docs/cellular_raza_building_blocks)
//! crate.
//! We will then solve our system for the
//! [`Mechanics`](https://cellular-raza.com/internals/concepts/cell/mechanics) aspect.
//!
//! ```
//! # use cellular_raza_core::backend::chili::*;
//! # use cellular_raza_core::{storage::*, time::*};
//! # use cellular_raza_concepts::*;
//! # use rand_chacha::ChaCha8Rng;
//! # use serde::{Deserialize, Serialize};
//! # use std::collections::{BTreeMap, BTreeSet};
//! # // Define agents and domain
//! # #[derive(Clone, Debug, Deserialize, Serialize)]
//! # struct MyAgent {
//! # pos: f64,
//! # vel: f64
//! # }
//! # impl Position<f64> for MyAgent {
//! # fn pos(&self) -> f64 {
//! # self.pos
//! # }
//! # fn set_pos(&mut self, pos: &f64) {
//! # self.pos = *pos
//! # }
//! # }
//! # impl Velocity<f64> for MyAgent {
//! # fn velocity(&self) -> f64 {
//! # self.vel
//! # }
//! # fn set_velocity(&mut self, vel: &f64) {
//! # self.vel = *vel
//! # }
//! # }
//! # impl Mechanics<f64, f64, f64> for MyAgent {
//! # fn get_random_contribution(
//! # &self,
//! # _rng: &mut ChaCha8Rng,
//! # _dt: f64,
//! # ) -> Result<(f64, f64), RngError> {
//! # Ok((0.0, 0.0))
//! # }
//! # fn calculate_increment(&self, force: f64) -> Result<(f64, f64), CalcError> {
//! # Ok((self.vel, force))
//! # }
//! # }
//! # #[derive(Clone, Debug, Deserialize, Serialize)]
//! # struct MyDomain {};
//! # impl<Ci> Domain<MyAgent, MyDomain, Ci> for MyDomain
//! # where
//! # Ci: IntoIterator<Item = MyAgent>
//! # {
//! # type VoxelIndex = usize;
//! # type SubDomainIndex = usize;
//! # fn decompose(
//! # self,
//! # _: core::num::NonZeroUsize,
//! # cells: Ci,
//! # ) -> Result<
//! # DecomposedDomain<Self::SubDomainIndex, MyDomain, MyAgent>,
//! # cellular_raza_concepts::DecomposeError,
//! # > {
//! # Ok(DecomposedDomain {
//! # n_subdomains: 1.try_into().unwrap(),
//! # index_subdomain_cells: vec![(1, MyDomain {}, cells.into_iter().collect())],
//! # neighbor_map: BTreeMap::from([(1, BTreeSet::new())]),
//! # rng_seed: 1,
//! # })
//! # }
//! # }
//! # impl SubDomain for MyDomain {
//! # type VoxelIndex = usize;
//! # fn get_neighbor_voxel_indices(&self, _: &Self::VoxelIndex) -> Vec<Self::VoxelIndex> {
//! # Vec::new()
//! # }
//! # fn get_all_indices(&self) -> Vec<Self::VoxelIndex> {
//! # vec![1]
//! # }
//! # }
//! # impl SortCells<MyAgent> for MyDomain {
//! # type VoxelIndex = usize;
//! # fn get_voxel_index_of(
//! # &self,
//! # _: &MyAgent,
//! # ) -> Result<Self::VoxelIndex, cellular_raza_concepts::BoundaryError> {
//! # Ok(1)
//! # }
//! # }
//! # impl SubDomainMechanics<f64, f64> for MyDomain {
//! # fn apply_boundary(
//! # &self,
//! # _: &mut f64,
//! # _: &mut f64,
//! # ) -> Result<(), cellular_raza_concepts::BoundaryError> {
//! # Ok(())
//! # }
//! # }
//! # let t0 = 0.0;
//! # let dt = 0.1;
//! # let tmax = 20.0;
//! # let save_interval = 2.0;
//! # let initial_vel = 0.1;
//! # let n_threads = 1.try_into().unwrap();
//! // Initialize agents, domain, solving time and how to store results
//! let agents = (0..10).map(|n| MyAgent {
//! /* Define the agent's properties */
//! # pos: n as f64,
//! # vel: initial_vel,
//! });
//! let domain = MyDomain {/* Define the domain*/};
//! let time = FixedStepsize::from_partial_save_interval(t0, dt, tmax, save_interval)?;
//! let storage = StorageBuilder::new().priority([StorageOption::Memory]);
//!
//! // Group them together
//! let settings = Settings {
//! n_threads,
//! time,
//! storage,
//! progressbar: None,
//! };
//!
//! // This will perform the numerical simulation
//! let storage_access = run_simulation!(
//! agents: agents,
//! domain: domain,
//! settings: settings,
//! aspects: [Mechanics],
//! core_path: cellular_raza_core,
//! )?;
//!
//! // Use calculated results
//! let history = storage_access.cells.load_all_elements()?;
//! for (iteration, cells) in history {
//! // ...
//! # assert!(iteration >= 0);
//! # assert_eq!(cells.len(), 10);
//! # for (_, (cbox, _)) in cells {
//! # if let CellIdentifier::Initial(key) = cbox.get_id() {
//! # let calculated = key as f64 + iteration as f64 * 0.1 * initial_vel;
//! # let cr = cbox.cell.pos;
//! # assert!((calculated - cr).abs() < 1e-5);
//! # } else {
//! # panic!("This should only contain initial identifiers");
//! # }
//! # }
//! }
//! # Ok::<(), SimulationError>(())
//! ```
//!
//! This example cannot contain all the functionality which the [chili](self) backend provides.
//! We encourage the user to look at the
//! [cellular-raza.com/guides](https://cellular-raza.com/guides) and
//! [cellular-raza.com/showcase](https://cellular-raza.com/showcase) sections to get started.
//!
//! # Internals
//! The [chili](self) backend uses procedural macros to generate code which
//! results in a fully working simulation.
//! The methods, functions and objects used in this way are formualted with generics.
//! This enables us to write general-purpose solvers for a wide range of problems.
//! The most important macro is the [run_simulation] macro which can be solely used to run
//! simulations.
//! This macro itself can be broken down into smaller pieces.
//! - [prepare_types] Defines types used in simulation
//! - [build_aux_storage] AuxStorage struct used to store intermediate
//! values for update steps of aspects
//! - [build_communicator] Type which handles communication between
//! threads
//! - [test_compatibility] Test compatibility of all types involved
//! - [run_main] Defines main loop and performs the simulation
//!
//! These macros take a subset of keyword arguments of the [run_simulation] macro.
//! The arguments are documented under the [run_simulation] macro.
//!
//! # Main Loop
//! The [run_main] macro constructs the main loop of the simulation.
//! It inserts functions depending on the given simulation aspects.
//! They can be grouped into 6 steps
//! Below, we show a list of all these functions and their corresponding aspects.
//!
//! #### Step 1 - Send Information
//!
//! In this step, each sub
//!
//! <style>
//! div table {
//! width: 100%;
//! }
//! table th:first-of-type {
//! width: 20%;
//! }
//! table th:nth-of-type(2) {
//! width: 35%;
//! }
//! table th:nth-of-type(3) {
//! width: 45%;
//! }
//! </style>
//!
//! | Aspects | Function | Purpose |
//! | --- | --- | --- |
//!
//! #### Step 2 - Calculate and Return
//! | Aspects | Function | Purpose |
//! | --- | --- | --- |
//!
//! #### Step 3 - Receive and Apply
//! | Aspects | Function | Purpose |
//! | --- | --- | --- |
//!
//! #### Pure Local Functions - Perform Update
//! | Aspects | Function | Purpose |
//! | --- | --- | --- |
//!
//! #### Step 4 - Treat Cell Positions
//! | Aspects | Function | Purpose |
//! | --- | --- | --- |
//!
//! #### Step 5 - Include new Cells
//! | Aspects | Function | Purpose |
//! | --- | --- | --- |
//!
//! # Return Type
//! After the simulation is done, we return a [StorageAccess] struct to interoperate with stored
//! results.
pub use ;
/// Contains structs to store aspects of the simulation and macros to construct them.
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;