-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBioFVM_basic_agent.cpp
More file actions
299 lines (246 loc) · 12.5 KB
/
Copy pathBioFVM_basic_agent.cpp
File metadata and controls
299 lines (246 loc) · 12.5 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
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
/*
#############################################################################
# If you use BioFVM in your project, please cite BioFVM and the version #
# number, such as below: #
# #
# We solved the diffusion equations using BioFVM (Version 1.1.6) [1] #
# #
# [1] A. Ghaffarizadeh, S.H. Friedman, and P. Macklin, BioFVM: an efficient #
# parallelized diffusive transport solver for 3-D biological simulations,#
# Bioinformatics 32(8): 1256-8, 2016. DOI: 10.1093/bioinformatics/btv730 #
# #
#############################################################################
# #
# BSD 3-Clause License (see https://opensource.org/licenses/BSD-3-Clause) #
# #
# Copyright (c) 2015-2017, Paul Macklin and the BioFVM Project #
# All rights reserved. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are #
# met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# #
# 2. Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# #
# 3. Neither the name of the copyright holder nor the names of its #
# contributors may be used to endorse or promote products derived from this #
# software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED #
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A #
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER #
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, #
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR #
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF #
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS #
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
# #
#############################################################################
*/
#include "BioFVM_basic_agent.h"
#include "BioFVM_agent_container.h"
#include "BioFVM_vector.h"
namespace BioFVM{
std::vector<Basic_Agent*> all_basic_agents(0);
Basic_Agent::Basic_Agent()
{
/*-----------------------------------------------------------------------------------------------------*/
/* give the agent a unique ID - but now there is no need for this as */
/* the ID is being generated in create_sources() or create_sinks() functions */
/* but if an agent is created directly i.e. Basic_Agent x = new Basic_Agent; */
/* then there will be a problem ! */
/* eepecially at non-root processes ! */
/*-----------------------------------------------------------------------------------------------------*/
static int max_basic_agent_ID = 0;
ID = max_basic_agent_ID; //
max_basic_agent_ID++;
is_active=true;
volume = 1.0;
position.assign( 3 , 0.0 ); // initialize position and velocity
velocity.assign( 3 , 0.0 );
previous_velocity.assign( 3 , 0.0 );
// link into the microenvironment, if one is defined
secretion_rates= new std::vector<double>(0);
uptake_rates= new std::vector<double>(0);
saturation_densities= new std::vector<double>(0);
extern Microenvironment* default_microenvironment;
register_microenvironment( default_microenvironment );
return;
}
void Basic_Agent::update_position(double dt){
//make sure to update current_voxel_index if you are implementing this function
};
/*---------------------------------------------------------------------------------------*/
/* This is a dummy function which calls another version of assign_position(...) function */
/* to set the x,y, and z coordinates of a Basic_Agent object */
/*---------------------------------------------------------------------------------------*/
bool Basic_Agent::assign_position(std::vector<double> new_position, int mpi_Rank, int *mpi_Dims) //-->Gaurav Saxena
{
return assign_position(new_position[0], new_position[1], new_position[2], mpi_Rank, mpi_Dims);
}
/*-----------------------------------------------------------------------------------*/
/* Sets x,y, and z position of the Basic_Agent on a specific process and also checks */
/* if the basic_agent has gone out of the domain. Further, it updates the voxel */
/* index in which the basic_agent now resides */
/*-----------------------------------------------------------------------------------*/
bool Basic_Agent::assign_position(double x, double y, double z, int mpi_Rank, int *mpi_Dims)
{
// std::cout << __FILE__ << " " << __LINE__ << std::endl;
if( !get_microenvironment()->mesh.is_position_valid(x,y,z)) //No need to parallelize is_position_valid(x,y,z) - it will work for parallel implementation
{
// std::cout<<"Error: the new position for agent "<< ID << " is invalid: "<<x<<","<<y<<","<<"z"<<std::endl;
return false;
}
position[0]=x;
position[1]=y;
position[2]=z;
update_voxel_index(mpi_Rank, mpi_Dims);
// make sure the agent is not already registered
get_microenvironment()->agent_container->register_agent(this);
return true;
}
/*---------------------------------------------------------------*/
/* The function checks if the basic_agent is still within domain */
/* If yes then it updates the current_voxel_index of basic_agent */
/* The current_voxel_index is the LOCAL index and NOT the GLOBAL */
/* index */
/*---------------------------------------------------------------*/
void Basic_Agent::update_voxel_index(int mpi_Rank, int *mpi_Dims)
{
if( !get_microenvironment()->mesh.is_position_valid(position[0],position[1],position[2]))
{
std::cout << "Invalid position detected " << position[0] << " " << position[1] << " " << position[2] << std::endl;
std::cout << "ID " << ID << std::endl;
current_voxel_index=-1;
is_active=false;
return;
}
int aux = get_microenvironment()->mesh.nearest_voxel_local_index( position, mpi_Rank, mpi_Dims );
if (aux < 0) aux = 0;
if (aux >= get_microenvironment()->mesh.voxels.size()) aux = get_microenvironment()->mesh.voxels.size() -1;
//current_voxel_index= microenvironment->mesh.nearest_voxel_local_index( position, mpi_Rank, mpi_Dims );
current_voxel_index = aux;
}
void Basic_Agent::set_internal_uptake_constants( double dt )
{
// overall form: dp/dt = S*(T-p) - U*p
// p(n+1) - p(n) = dt*S(n)*T(n) - dt*( S(n) + U(n))*p(n+1)
// p(n+1)*temp2 = p(n) + temp1
// p(n+1) = ( p(n) + temp1 )/temp2
//int nearest_voxel= current_voxel_index;
double internal_constant_to_discretize_the_delta_approximation = dt * volume / ( (microenvironment->voxels(current_voxel_index)).volume ) ; // needs a fix
// temp1 = dt*(V_cell/V_voxel)*S*T
cell_source_sink_solver_temp1.assign( (*secretion_rates).size() , 0.0 );
cell_source_sink_solver_temp1 += *secretion_rates;
cell_source_sink_solver_temp1 *= *saturation_densities;
cell_source_sink_solver_temp1 *= internal_constant_to_discretize_the_delta_approximation;
// temp2 = 1 + dt*(V_cell/V_voxel)*( S + U )
cell_source_sink_solver_temp2.assign( (*secretion_rates).size() , 1.0 );
axpy( &(cell_source_sink_solver_temp2) , internal_constant_to_discretize_the_delta_approximation , *secretion_rates );
axpy( &(cell_source_sink_solver_temp2) , internal_constant_to_discretize_the_delta_approximation , *uptake_rates );
volume_is_changed = false;
}
void Basic_Agent::register_microenvironment( Microenvironment* microenvironment_in )
{
microenvironment = microenvironment_in;
secretion_rates->resize( microenvironment->number_of_densities() , 0.0 );
saturation_densities->resize( microenvironment->number_of_densities() , 0.0 );
uptake_rates->resize( microenvironment->number_of_densities() , 0.0 );
// some solver temporary variables
cell_source_sink_solver_temp1.resize( microenvironment->number_of_densities() , 0.0 );
cell_source_sink_solver_temp2.resize( microenvironment->number_of_densities() , 1.0 );
return;
}
Microenvironment* Basic_Agent::get_microenvironment( void )
{ return microenvironment; }
Basic_Agent::~Basic_Agent()
{
return;
}
Basic_Agent* create_basic_agent( void )
{
Basic_Agent* pNew;
pNew = new Basic_Agent;
all_basic_agents.push_back( pNew );
pNew->index=all_basic_agents.size()-1; //index is local to this process
return pNew;
}
void delete_basic_agent( int index )
{
// deregister agent in microenvironment
all_basic_agents[index]->get_microenvironment()->agent_container->remove_agent(all_basic_agents[index]);
// de-allocate (delete) the Basic_Agent;
delete all_basic_agents[index];
// next goal: remove this memory address.
// performance goal: don't delete in the middle -- very expensive reallocation
// alternative: copy last element to index position, then shrink vector by 1 at the end O(constant)
// move last item to index location
all_basic_agents[ all_basic_agents.size()-1 ]->index=index;
all_basic_agents[index] = all_basic_agents[ all_basic_agents.size()-1 ];
// shrink the vector
all_basic_agents.pop_back();
return;
}
void delete_basic_agent( Basic_Agent* pDelete )
{
// First, figure out the index of this agent. This is not efficient.
// int delete_index = 0;
// while( all_basic_agents[ delete_index ] != pDelete )
// { delete_index++; }
delete_basic_agent(pDelete->index);
return;
}
int Basic_Agent::get_current_voxel_index( void )
{
return current_voxel_index;
}
double *Basic_Agent::nearest_density_vector( void )
{
return microenvironment->nearest_density_vector( current_voxel_index );
}
// directly access the gradient of substrate n nearest to the cell
std::vector<double>& Basic_Agent::nearest_gradient( int substrate_index )
{
return microenvironment->gradient_vector(current_voxel_index)[substrate_index];
}
// directly access a vector of gradients, one gradient per substrate
std::vector<gradient>& Basic_Agent::nearest_gradient_vector( void )
{
return microenvironment->gradient_vector(current_voxel_index);
}
void Basic_Agent::set_total_volume(double volume)
{
this->volume = volume;
volume_is_changed = true;
}
double Basic_Agent::get_total_volume()
{
return volume;
}
void Basic_Agent::simulate_secretion_and_uptake( Microenvironment* pS, double dt )
{
if(!is_active)
{ return; }
if( volume_is_changed )
{
set_internal_uptake_constants(dt);
volume_is_changed = false;
}
int d_size = (*pS).number_of_densities();
for(int d = 0; d < d_size; ++d) {
(*pS)(current_voxel_index)[d] += cell_source_sink_solver_temp1[d];
}
for(int d = 0; d < d_size; ++d) {
(*pS)(current_voxel_index)[d] /= cell_source_sink_solver_temp2[d];
}
return;
}
};