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
4344namespace bdm {
4445
4546int 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}
0 commit comments