-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdish.cpp
More file actions
executable file
·153 lines (131 loc) · 3.9 KB
/
dish.cpp
File metadata and controls
executable file
·153 lines (131 loc) · 3.9 KB
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
#include "dish.h"
#include "diffusible.h"
#define REFLECTIVE_BC 0
#define ABSORVING_BC 1
#define INITIAL_BC 2
Dish::Dish(){
N = 100;
dim = 0;
dt = 0.001;
t = 0;
L = 10;
}
Dish::Dish(Population& colony_, int N_, double L_){
N = N_;
colony = &colony_;
t = 0;
dt = 0.001;
L = L_;
}
void Dish::add_chemical(std::string name, double conc, double coeff, double decay){
diffusibles.emplace_back(name,coeff,decay,conc,N,L); // adding diffusible constants
}
void Dish::set_chemical_gaussianprofile(std::string name_, double x_c, double y_c, double max, double sigma){
bool success = false;
double x,y;
for (auto& diffusible: diffusibles){
if (diffusible.name == name_){
success = true;
for(int i=0; i<N; i++){
for (int j=0; j<N; j++){
x = grid_to_dish_x(j);
y = grid_to_dish_y(i);
gsl_matrix_set(diffusible.conc,i,j,max*exp(-((x-x_c)*(x-x_c)+(y-y_c)*(y-y_c))/(2*sigma*sigma)));
}
}
}
}
if(success == false){
std::cout<<"WARNING: Chemical species "<<name_<<" not found in Dish"<<'\n';
}
}
void Dish::link_chemical_bacterium(std::string chem_out, std::string chem_in){
bool success = false;
for (auto& diffusible: diffusibles){
if (diffusible.name == chem_out){
success = colony->link_diffusible_bacterium(chem_in, &diffusible);
}
}
if(success == false){
std::cout<<"WARNING: Not found pair of chemicals to link "<<chem_out<<' '<<chem_in<<'\n';
}
}
void Dish::evolve(){
colony->evolve(); // eventually some rules on timesteps should be set
t += dt;
for(auto& M: diffusibles){
if(boundary_condition == REFLECTIVE_BC){ // if reflective boundaries set same vaue in two row/columns from the border
gsl_matrix_memcpy(&M.row0.matrix,&M.row1.matrix);
gsl_matrix_memcpy(&M.col0.matrix,&M.col1.matrix);
gsl_matrix_memcpy(&M.rowN.matrix,&M.rowNm.matrix);
gsl_matrix_memcpy(&M.colN.matrix,&M.colNm.matrix);
}
gsl_matrix_memcpy(M.auxconc,M.conc); // two matrices to avoid overwriting in incremental sum
///// Implementation of finite difference Laplacian
gsl_matrix_scale(M.auxconc,M.diff_coeff*L*L/N/N*dt);
gsl_matrix_add(&M.centre.matrix,&M.auxleft.matrix);
gsl_matrix_add(&M.centre.matrix,&M.auxright.matrix);
gsl_matrix_add(&M.centre.matrix,&M.auxtop.matrix);
gsl_matrix_add(&M.centre.matrix,&M.auxbottom.matrix);
gsl_matrix_scale(M.auxconc,4.0);
gsl_matrix_sub(&M.centre.matrix,&M.auxcentre.matrix);
//// Degradation of diffusible
gsl_matrix_scale(&M.centre.matrix,(1-M.decay_coeff*dt));
// difusible reaction loop
//...
}
}
void Dish::set_reflective_boundary(){
boundary_condition = REFLECTIVE_BC;
}
void Dish::set_absorving_boundary(){
boundary_condition = ABSORVING_BC;
for(auto& M: diffusibles){
gsl_matrix_set_all(&M.row0.matrix,0);
gsl_matrix_set_all(&M.rowN.matrix,0);
gsl_matrix_set_all(&M.col0.matrix,0);
gsl_matrix_set_all(&M.colN.matrix,0);
}
}
void Dish::set_constant_boundary(){
boundary_condition = INITIAL_BC;
for(auto& M: diffusibles){
gsl_matrix_memcpy(&M.row0.matrix,&M.row1.matrix);
gsl_matrix_memcpy(&M.col0.matrix,&M.col1.matrix);
gsl_matrix_memcpy(&M.rowN.matrix,&M.rowNm.matrix);
gsl_matrix_memcpy(&M.colN.matrix,&M.colNm.matrix);
}
}
double Dish::grid_to_dish_x(int j){
return L/N*(0.5+j) - L/2;
}
double Dish::grid_to_dish_y(int i){
return L/2 - L/N*(0.5+i);
}
int Dish::dish_to_grid_j(double x){
return floor((x-L/2)/(L/N));//
}
int Dish::dish_to_grid_i(double y){
return floor((L/2-y)/(L/N));//
}
void Dish::save(){
for(int i = 0; i<diffusibles.size(); i++){
std::filesystem::create_directory("output/diffusible");
ofilename.str("");
std::cout<<"t:"<<t<<'\n';
ofilename<<"output/diffusible/diffusible_"<<i<<'_'<<std::setprecision(5)<<t<<".out";
trajfile.open(ofilename.str()); // output trajectory
for(int j=0; j<N; j++){
for(int k=0;k<N; k++){
trajfile<<gsl_matrix_get(diffusibles[i].conc,j,k);
if(k<N-1){
trajfile<<' ';
}
}
if(j<N-1){
trajfile<<'\n';
}
}
trajfile.close();
}
}