Skip to content

Commit 470a14b

Browse files
authored
Add interface file (#8)
1 parent 6601f3e commit 470a14b

14 files changed

+1608
-471
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,10 @@ SpacesInSquareBrackets: false
9191
Standard: Auto
9292
TabWidth: 8
9393
UseTab: Never
94+
---
95+
Language: Json
96+
IndentWidth: 2
97+
UseTab: Never
98+
ColumnLimit: 80
9499
...
95100

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,8 @@ doc/latex
123123
# Draft code for comparing models
124124
draft_code_my_own_analysis/
125125

126+
# Python environment
127+
.venv/
128+
126129
# Temporary files
127130
cout.txt

CARTopiaX_Simulation_Analysis.ipynb

Lines changed: 380 additions & 0 deletions
Large diffs are not rendered by default.

params.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"seed": 1,
3+
"output_performance_statistics": true,
4+
"total_minutes_to_simulate": 43200,
5+
"initial_tumor_radius": 150.0,
6+
"treatment": {
7+
"0": 3957,
8+
"8": 3957
9+
}
10+
}

src/cart_cell.cc

Lines changed: 72 additions & 53 deletions
Large diffs are not rendered by default.

src/cart_tumor.cc

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,40 @@
3636
#include "core/resource_manager.h"
3737
#include "core/scheduler.h"
3838
#include "core/simulation.h"
39+
#include <cstdint>
3940
#include <iostream>
4041
#include <memory>
4142
#include <vector>
4243

4344
namespace bdm {
4445

4546
int Simulate(int argc, const char** argv) {
46-
// Set simulation bounds
47-
auto set_param = [](Param* param) {
48-
// Set a fixed random seed for reproducibility
49-
param->random_seed = kSeed;
50-
// Periodic boundary conditions
47+
// Load parameters from JSON file or use default values
48+
std::unique_ptr<SimParam> custom_parameters = std::make_unique<SimParam>();
49+
custom_parameters->LoadParams("params.json");
50+
51+
// Keep a reference to the parameters before releasing
52+
const auto* sparam_ref = custom_parameters.get();
53+
54+
// Transfer ownership to BioDynaMo parameter system
55+
Param::RegisterParamGroup(custom_parameters.release());
56+
57+
// Set simulation parameters using lambda
58+
auto set_param = [sparam_ref](Param* param) {
59+
// Set simulation bounds using the parameters
60+
param->random_seed = sparam_ref->seed;
5161
param->bound_space = Param::BoundSpaceMode::kTorus;
52-
// Cube of kBoundedSpaceLength³ centered at origin
53-
param->min_bound = -kBoundedSpaceLength / kHalf;
54-
param->max_bound = kBoundedSpaceLength / kHalf;
55-
param->simulation_time_step = kDt;
56-
// for outputing performance statistics
57-
param->statistics = kOutputPerformanceStatistics;
62+
param->min_bound = -sparam_ref->bounded_space_length / kHalf;
63+
param->max_bound = sparam_ref->bounded_space_length / kHalf;
64+
param->simulation_time_step = sparam_ref->dt_step;
65+
param->statistics = sparam_ref->output_performance_statistics;
5866
};
5967

6068
Simulation simulation(argc, argv, set_param);
69+
const auto* sparam = simulation.GetParam()->Get<SimParam>();
70+
// Print parameters
71+
sparam->PrintParams();
72+
6173
ExecutionContext* ctxt = simulation.GetExecutionContext();
6274

6375
// Change Forces
@@ -72,7 +84,7 @@ int Simulate(int argc, const char** argv) {
7284
auto* env = dynamic_cast<UniformGridEnvironment*>(
7385
Simulation::GetActive()->GetEnvironment());
7486
// Fix the box length for the uniform grid environment
75-
env->SetBoxLength(gKLengthBoxMechanics);
87+
env->SetBoxLength(sparam->length_box_mechanics);
7688

7789
// Define Substances
7890
ResourceManager* rm = Simulation::GetActive()->GetResourceManager();
@@ -82,8 +94,9 @@ int Simulate(int argc, const char** argv) {
8294
// time_step
8395
std::unique_ptr<DiffusionThomasAlgorithm> oxygen_grid =
8496
std::make_unique<DiffusionThomasAlgorithm>(
85-
kOxygen, "oxygen", kDiffusionCoefficientOxygen, kDecayConstantOxygen,
86-
kResolutionGridSubstances, kDtSubstances,
97+
kOxygen, "oxygen", sparam->diffusion_coefficient_oxygen,
98+
sparam->decay_constant_oxygen, sparam->resolution_grid_substances,
99+
sparam->dt_substances,
87100
/*dirichlet_border=*/true);
88101
rm->AddContinuum(oxygen_grid.release());
89102

@@ -92,9 +105,9 @@ int Simulate(int argc, const char** argv) {
92105
std::unique_ptr<DiffusionThomasAlgorithm> immunostimulatory_factor_grid =
93106
std::make_unique<DiffusionThomasAlgorithm>(
94107
kImmunostimulatoryFactor, "immunostimulatory_factor",
95-
kDiffusionCoefficientImmunostimulatoryFactor,
96-
kDecayConstantImmunostimulatoryFactor, kResolutionGridSubstances,
97-
kDtSubstances,
108+
sparam->diffusion_coefficient_immunostimulatory_factor,
109+
sparam->decay_constant_immunostimulatory_factor,
110+
sparam->resolution_grid_substances, sparam->dt_substances,
98111
/*dirichlet_border=*/false);
99112
rm->AddContinuum(immunostimulatory_factor_grid.release());
100113

@@ -103,9 +116,10 @@ int Simulate(int argc, const char** argv) {
103116
// Oxygen comming from the borders (capillary vessels)
104117
ModelInitializer::AddBoundaryConditions(
105118
kOxygen, BoundaryConditionType::kDirichlet,
106-
// kOxygenReferenceLevel mmHg is the physiological level of oxygen in
119+
// oxygen_reference_level mmHg is the physiological level of oxygen in
107120
// tissues, o2 saturation is 100% at this level
108-
std::make_unique<ConstantBoundaryCondition>(kOxygenReferenceLevel));
121+
std::make_unique<ConstantBoundaryCondition>(
122+
sparam->oxygen_reference_level));
109123

110124
// This is useless now but should be added this way in a future version of
111125
// BioDynaMo
@@ -114,15 +128,15 @@ int Simulate(int argc, const char** argv) {
114128

115129
// Initialize oxygen voxels
116130
ModelInitializer::InitializeSubstance(
117-
kOxygen, [](real_t /*x*/, real_t /*y*/, real_t /*z*/) {
118-
// Set all voxels to kInitialOxygenLevel mmHg
119-
return kInitialOxygenLevel;
131+
kOxygen, [sparam](real_t /*x*/, real_t /*y*/, real_t /*z*/) {
132+
// Set all voxels to initial_oxygen_level mmHg
133+
return sparam->initial_oxygen_level;
120134
});
121135

122-
// One spherical tumor of radius kInitialRadiusTumor in the center of the
136+
// One spherical tumor of radius initial_tumor_radius in the center of the
123137
// simulation space
124138
const std::vector<Real3> positions =
125-
CreateSphereOfTumorCells(kInitialRadiusTumor);
139+
CreateSphereOfTumorCells(sparam->initial_tumor_radius);
126140
for (const auto& pos : positions) {
127141
std::unique_ptr<TumorCell> tumor_cell = std::make_unique<TumorCell>(pos);
128142
std::unique_ptr<StateControlGrowProliferate> state_control =
@@ -133,23 +147,26 @@ int Simulate(int argc, const char** argv) {
133147

134148
// Treatment administration operation
135149
std::unique_ptr<bdm::Operation> treatment_op =
136-
std::make_unique<bdm::Operation>("SpawnCart", kStepsOneDay);
150+
std::make_unique<bdm::Operation>("SpawnCart", sparam->steps_in_one_day);
137151
std::unique_ptr<bdm::SpawnCart> spawn_cart =
138152
std::make_unique<bdm::SpawnCart>();
139153
treatment_op->AddOperationImpl(bdm::kCpu, spawn_cart.release());
140154
scheduler->ScheduleOp(treatment_op.release());
141155

142156
// OutputSummary operation
143-
std::unique_ptr<bdm::Operation> summary_op =
144-
std::make_unique<bdm::Operation>("OutputSummary", kOutputCsvInterval);
157+
std::unique_ptr<bdm::Operation> summary_op = std::make_unique<bdm::Operation>(
158+
"OutputSummary", sparam->output_csv_interval);
145159
std::unique_ptr<bdm::OutputSummary> output_summary =
146160
std::make_unique<bdm::OutputSummary>();
147161
summary_op->AddOperationImpl(bdm::kCpu, output_summary.release());
148162
scheduler->ScheduleOp(summary_op.release());
149163

150164
// Run simulation
151-
// simulate kTotalMinutesToSimulate minutes including the last minute
152-
scheduler->Simulate(1 + kTotalMinutesToSimulate / kDt);
165+
std::cout << "Running simulation..." << std::endl;
166+
// simulate total_minutes_to_simulate minutes including the last minute
167+
scheduler->Simulate(1 +
168+
static_cast<uint64_t>(sparam->total_minutes_to_simulate /
169+
sparam->dt_step));
153170
std::cout << "Simulation completed successfully!" << std::endl;
154171
return 0;
155172
}

src/diffusion_thomas_algorithm.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "core/agent/agent.h"
2727
#include "core/container/math_array.h"
2828
#include "core/diffusion/diffusion_grid.h"
29+
#include "core/param/param.h"
2930
#include "core/real_t.h"
3031
#include "core/resource_manager.h"
3132
#include <cstddef>
@@ -44,12 +45,15 @@ DiffusionThomasAlgorithm::DiffusionThomasAlgorithm(int substance_id,
4445
: DiffusionGrid(substance_id, std::move(substance_name), dc, mu,
4546
resolution),
4647
resolution_(static_cast<int>(GetResolution())),
47-
d_space_(static_cast<real_t>(kBoundedSpaceLength) /
48+
d_space_(static_cast<real_t>(Simulation::GetActive()
49+
->GetParam()
50+
->Get<SimParam>()
51+
->bounded_space_length) /
4852
static_cast<real_t>(resolution_)),
4953
dirichlet_border_(dirichlet_border),
5054
jump_i_(1),
5155
jump_j_(resolution_),
52-
jump_k_(resolution_ * resolution_),
56+
jump_(resolution_ * resolution_),
5357
spatial_diffusion_coeff_(dc * dt / (d_space_ * d_space_)),
5458
neg_diffusion_factor_(-spatial_diffusion_coeff_),
5559
temporal_decay_coeff_(mu * dt / 3.0),
@@ -63,7 +67,6 @@ DiffusionThomasAlgorithm::DiffusionThomasAlgorithm(int substance_id,
6367
thomas_c_z_(resolution_, neg_diffusion_factor_),
6468
thomas_denom_z_(resolution_, central_coeff_) {
6569
SetTimeStep(dt);
66-
6770
// Initialize the denominators and coefficients for the Thomas algorithm
6871
InitializeThomasAlgorithmVectors(thomas_denom_x_, thomas_c_x_);
6972
InitializeThomasAlgorithmVectors(thomas_denom_y_, thomas_c_y_);
@@ -245,7 +248,7 @@ void DiffusionThomasAlgorithm::SolveDirectionThomas(int direction) {
245248
return jump_j_;
246249
}
247250
// direction == 2
248-
return jump_k_;
251+
return jump_;
249252
}();
250253

251254
#pragma omp parallel for collapse(2)

src/diffusion_thomas_algorithm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DiffusionThomasAlgorithm : public DiffusionGrid {
4343
dirichlet_border_(false),
4444
jump_i_(0),
4545
jump_j_(0),
46-
jump_k_(0),
46+
jump_(0),
4747
spatial_diffusion_coeff_(0.0),
4848
neg_diffusion_factor_(0.0),
4949
temporal_decay_coeff_(0.0),
@@ -138,7 +138,7 @@ class DiffusionThomasAlgorithm : public DiffusionGrid {
138138
int jump_j_;
139139

140140
/// Index jump for k-direction (z-axis)
141-
int jump_k_;
141+
int jump_;
142142

143143
/// First diffusion constant
144144
real_t spatial_diffusion_coeff_;

src/forces_tumor_cart.cc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "core/agent/cell.h"
2828
#include "core/container/math_array.h"
2929
#include "core/interaction_force.h"
30+
#include "core/param/param.h"
3031
#include "core/real_t.h"
3132
#include <algorithm>
3233
#include <cmath>
@@ -43,18 +44,23 @@ Real4 InteractionVelocity::Calculate(const Agent* lhs, const Agent* rhs) const {
4344
return {0.0, 0.0, 0.0, 0.0};
4445
}
4546

47+
const auto* sparams = Simulation::GetActive()->GetParam()->Get<SimParam>();
48+
4649
Real3 displacement = a->GetPosition() - b->GetPosition();
4750

4851
// For periodic boundary conditions, we need to adjust the displacement
4952
displacement[0] =
5053
displacement[0] -
51-
(kBoundedSpaceLength)*round(displacement[0] / (kBoundedSpaceLength));
54+
(sparams->bounded_space_length) *
55+
round(displacement[0] / (sparams->bounded_space_length));
5256
displacement[1] =
5357
displacement[1] -
54-
(kBoundedSpaceLength)*round(displacement[1] / (kBoundedSpaceLength));
58+
(sparams->bounded_space_length) *
59+
round(displacement[1] / (sparams->bounded_space_length));
5560
displacement[2] =
5661
displacement[2] -
57-
(kBoundedSpaceLength)*round(displacement[2] / (kBoundedSpaceLength));
62+
(sparams->bounded_space_length) *
63+
round(displacement[2] / (sparams->bounded_space_length));
5864

5965
const real_t dist_sq = displacement[0] * displacement[0] +
6066
displacement[1] * displacement[1] +
@@ -79,23 +85,25 @@ Real4 InteractionVelocity::Calculate(const Agent* lhs, const Agent* rhs) const {
7985

8086
if ((a_tumor != nullptr) && (b_tumor != nullptr)) {
8187
// two tumor cells
82-
// std::sqrt(kRepulsionTumorTumor * kRepulsionTumorTumor);
83-
repulsion = kRepulsionTumorTumor;
88+
// std::sqrt(sparams->cell_repulsion_between_tumor_tumor *
89+
// sparams->cell_repulsion_between_tumor_tumor);
90+
repulsion = sparams->cell_repulsion_between_tumor_tumor;
8491
} else if ((a_tumor == nullptr) && (b_tumor == nullptr)) {
8592
// two CAR-T cells
86-
// std::sqrt(kRepulsionCartCart*kRepulsionCartCart);
87-
repulsion = kRepulsionCartCart;
93+
// std::sqrt(sparams->cell_repulsion_between_cart_cart*sparams->cell_repulsion_between_cart_cart);
94+
repulsion = sparams->cell_repulsion_between_cart_cart;
8895
} else {
8996
// one tumor cell and one CAR-T
90-
repulsion = std::sqrt(kRepulsionCartTumor * kRepulsionTumorCart);
97+
repulsion = std::sqrt(sparams->cell_repulsion_between_cart_tumor *
98+
sparams->cell_repulsion_between_tumor_cart);
9199
}
92100

93101
temp_r *= repulsion;
94102
}
95103

96104
// Adhesion
97105
const real_t max_interaction_distance =
98-
kMaxRelativeAdhesionDistance * combined_radius;
106+
sparams->max_relative_adhesion_distance * combined_radius;
99107

100108
if (distance < max_interaction_distance) {
101109
// 1 - d/S
@@ -106,13 +114,14 @@ Real4 InteractionVelocity::Calculate(const Agent* lhs, const Agent* rhs) const {
106114
real_t adhesion = NAN;
107115
if ((a_tumor != nullptr) && (b_tumor != nullptr)) {
108116
// two tumor cells
109-
adhesion = kAdhesionTumorTumor;
117+
adhesion = sparams->cell_adhesion_between_tumor_tumor;
110118
} else if ((a_tumor == nullptr) && (b_tumor == nullptr)) {
111119
// two CAR-T cells
112-
adhesion = kAdhesionCartCart;
120+
adhesion = sparams->cell_adhesion_between_cart_cart;
113121
} else {
114122
// one tumor cell and one CAR-T
115-
adhesion = std::sqrt(kAdhesionCartTumor * kAdhesionTumorCart);
123+
adhesion = std::sqrt(sparams->cell_adhesion_between_cart_tumor *
124+
sparams->cell_adhesion_between_tumor_cart);
116125
}
117126

118127
temp_a *= adhesion;

0 commit comments

Comments
 (0)