-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathBioFVM_microenvironment.cpp
More file actions
1738 lines (1456 loc) · 60.6 KB
/
BioFVM_microenvironment.cpp
File metadata and controls
1738 lines (1456 loc) · 60.6 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
#############################################################################
# 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.7) [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-2025, 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_microenvironment.h"
#include "BioFVM_solvers.h"
#include "BioFVM_vector.h"
#include <cmath>
#include <algorithm>
#include "BioFVM_basic_agent.h"
namespace BioFVM{
extern std::string BioFVM_version;
extern std::string BioFVM_URL;
Microenvironment* default_microenvironment = NULL;
void set_default_microenvironment( Microenvironment* M )
{ default_microenvironment = M; }
Microenvironment* get_default_microenvironment( void )
{ return default_microenvironment; }
void zero_function( std::vector<double>& position, std::vector<double>& input , std::vector<double>* write_destination )
{
for( unsigned int i=0 ; i < write_destination->size() ; i++ )
{ (*write_destination)[i] = 0.0; }
return;
}
void one_function( std::vector<double>& position, std::vector<double>& input , std::vector<double>* write_destination )
{
for( unsigned int i=0 ; i < write_destination->size() ; i++ )
{ (*write_destination)[i] = 1.0; }
return;
}
void zero_function( Microenvironment* pMicroenvironment, int voxel_index, std::vector<double>* write_destination )
{
for( unsigned int i=0 ; i < write_destination->size() ; i++ )
{ (*write_destination)[i] = 0.0; }
return;
}
void one_function( Microenvironment* pMicroenvironment, int voxel_index, std::vector<double>* write_destination )
{
for( unsigned int i=0 ; i < write_destination->size() ; i++ )
{ (*write_destination)[i] = 1.0; }
return;
}
void empty_diffusion_solver( Microenvironment& S, double dt )
{
static bool setup_done = false;
if( !setup_done )
{
std::cout << "Using the empty diffusion solver ... " << std::endl;
setup_done = true;
}
return;
}
Microenvironment::Microenvironment()
{
name = "unnamed";
spatial_units = "none";
time_units = "none";
bulk_source_sink_solver_setup_done = false;
thomas_setup_done = false;
diffusion_solver_setup_done = false;
diffusion_decay_solver = empty_diffusion_solver;
diffusion_decay_solver = diffusion_decay_solver__constant_coefficients_LOD_3D;
mesh.resize(1,1,1);
one.resize( 1 , 1.0 );
zero.resize( 1 , 0.0 );
temporary_density_vectors1.resize( mesh.voxels.size() , zero );
temporary_density_vectors2.resize( mesh.voxels.size() , zero );
p_density_vectors = &temporary_density_vectors1;
gradient_vectors.resize( mesh.voxels.size() );
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( 1 );
(gradient_vectors[k])[0].resize( 3, 0.0 );
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
bulk_supply_rate_function = zero_function;
bulk_supply_target_densities_function = zero_function;
bulk_uptake_rate_function = zero_function;
density_names.assign( 1 , "unnamed" );
density_units.assign( 1 , "none" );
diffusion_coefficients.assign( number_of_densities() , 0.0 );
decay_rates.assign( number_of_densities() , 0.0 );
one_half = one;
one_half *= 0.5;
one_third = one;
one_third /= 3.0;
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vector.assign( 1 , false );
dirichlet_activation_vectors.assign( 1 , dirichlet_activation_vector );
default_microenvironment_options.Dirichlet_all.assign( 1 , true );
default_microenvironment_options.Dirichlet_xmin.assign( 1 , false );
default_microenvironment_options.Dirichlet_xmax.assign( 1 , false );
default_microenvironment_options.Dirichlet_ymin.assign( 1 , false );
default_microenvironment_options.Dirichlet_ymax.assign( 1 , false );
default_microenvironment_options.Dirichlet_zmin.assign( 1 , false );
default_microenvironment_options.Dirichlet_zmax.assign( 1 , false );
default_microenvironment_options.Dirichlet_xmin_values.assign( 1 , 1.0 );
default_microenvironment_options.Dirichlet_xmax_values.assign( 1 , 1.0 );
default_microenvironment_options.Dirichlet_ymin_values.assign( 1 , 1.0 );
default_microenvironment_options.Dirichlet_ymax_values.assign( 1 , 1.0 );
default_microenvironment_options.Dirichlet_zmin_values.assign( 1 , 1.0 );
default_microenvironment_options.Dirichlet_zmax_values.assign( 1 , 1.0 );
if(default_microenvironment==NULL)
{ default_microenvironment=this; }
return;
}
Microenvironment::Microenvironment(std::string name)
{
Microenvironment();
this->name=name;
return;
}
void Microenvironment::fix_substrate_at_voxel( std::string substrate, int voxel_index, double new_value )
{
int substrate_index = find_existing_density_index( substrate );
return fix_substrate_at_voxel(substrate_index, voxel_index, new_value);
}
void Microenvironment::fix_substrate_at_voxel( std::string substrate, int voxel_index )
{
int substrate_index = find_existing_density_index( substrate );
return fix_substrate_at_voxel(substrate_index, voxel_index);
}
void Microenvironment::fix_substrate_at_voxels( std::string substrate, std::vector<int>& voxel_indices, double new_value )
{
int substrate_index = find_existing_density_index( substrate );
return fix_substrate_at_voxels(substrate_index, voxel_indices, new_value);
}
void Microenvironment::fix_substrate_at_voxels( std::string substrate, std::vector<int>& voxel_indices, std::vector<double>& new_values )
{
int substrate_index = find_existing_density_index( substrate );
return fix_substrate_at_voxels(substrate_index, voxel_indices, new_values);
}
void Microenvironment::fix_substrate_at_voxels( std::string substrate, std::vector<int>& voxel_indices )
{
int substrate_index = find_existing_density_index( substrate );
return fix_substrate_at_voxels(substrate_index, voxel_indices);
}
void Microenvironment::fix_substrate_at_voxel( int substrate_index, int voxel_index, double new_value )
{
dirichlet_value_vectors[voxel_index][substrate_index] = new_value;
fix_substrate_at_voxel(substrate_index, voxel_index);
}
void Microenvironment::fix_substrate_at_voxel( int substrate_index, int voxel_index )
{
dirichlet_activation_vectors[voxel_index][substrate_index] = true;
mesh.voxels[voxel_index].is_Dirichlet = true;
dirichlet_activation_vector[substrate_index] = true;
return;
}
void Microenvironment::fix_substrates_at_voxel( int voxel_index , std::vector<double>& new_values )
{
if (new_values.size() != dirichlet_value_vectors[voxel_index].size())
{
std::cerr << "Error: Incorrect number of values passed in to fix_substrates_at_voxel. Expected " << dirichlet_value_vectors[voxel_index].size() << " values, but got " << new_values.size() << "." << std::endl;
return;
}
dirichlet_value_vectors[voxel_index] = new_values;
return fix_substrates_at_voxel( voxel_index );
}
void Microenvironment::fix_substrates_at_voxel( int voxel_index )
{
for( unsigned int i=0 ; i < dirichlet_activation_vectors[voxel_index].size() ; i++ )
{
fix_substrate_at_voxel( i , voxel_index );
}
return;
}
void Microenvironment::fix_substrate_at_voxels( int substrate_index, std::vector<int>& voxel_indices, double new_value )
{
for( auto voxel_index : voxel_indices )
{
dirichlet_value_vectors[voxel_index][substrate_index] = new_value;
}
return fix_substrate_at_voxels( substrate_index, voxel_indices );
}
void Microenvironment::fix_substrate_at_voxels( int substrate_index, std::vector<int>& voxel_indices, std::vector<double>& new_values )
{
if (new_values.size() != voxel_indices.size())
{
std::cerr << "Error: new_values size (" << new_values.size() << ") does not match voxel_indices size (" << voxel_indices.size() << ") in Microenvironment::fix_substrate_at_voxels" << std::endl;
return;
}
for( unsigned int i=0 ; i < voxel_indices.size() ; i++ )
{
dirichlet_value_vectors[voxel_indices[i]][substrate_index] = new_values[i];
}
return fix_substrate_at_voxels( substrate_index, voxel_indices );
}
void Microenvironment::fix_substrate_at_voxels( int substrate_index, std::vector<int>& voxel_indices )
{
for( auto voxel_index : voxel_indices )
{
dirichlet_activation_vectors[voxel_index][substrate_index] = true;
mesh.voxels[voxel_index].is_Dirichlet = true;
}
// do not allow dirichlet_activation_vector[substrate_index] to be set to true if no voxels are changed
if (voxel_indices.size() > 0)
{
dirichlet_activation_vector[substrate_index] = true;
}
return;
}
void Microenvironment::unfix_substrate_at_voxel( std::string substrate, int voxel_index )
{
int substrate_index = find_existing_density_index( substrate );
return unfix_substrate_at_voxel(substrate_index, voxel_index);
}
void Microenvironment::unfix_substrate_at_voxels( std::string substrate, std::vector<int>& voxel_indices )
{
int substrate_index = find_existing_density_index( substrate );
return unfix_substrate_at_voxels(substrate_index, voxel_indices);
}
void Microenvironment::unfix_substrate_at_voxel( int substrate_index, int voxel_index )
{
dirichlet_activation_vectors[voxel_index][substrate_index] = false;
if (std::find(dirichlet_activation_vectors[voxel_index].begin(), dirichlet_activation_vectors[voxel_index].end(), true) == dirichlet_activation_vectors[voxel_index].end())
{
mesh.voxels[voxel_index].is_Dirichlet = false;
}
sync_substrate_dirichlet_activation(substrate_index);
return;
}
void Microenvironment::unfix_substrate_at_voxels( int substrate_index, std::vector<int>& voxel_indices )
{
// this allows for unfixing a substrate at multiple voxels at once without checking if the substrate remains a DC substrate after removing from each voxel
for( auto voxel_index : voxel_indices )
{
dirichlet_activation_vectors[voxel_index][substrate_index] = false;
if (std::find(dirichlet_activation_vectors[voxel_index].begin(), dirichlet_activation_vectors[voxel_index].end(), true) == dirichlet_activation_vectors[voxel_index].end())
{
mesh.voxels[voxel_index].is_Dirichlet = false;
}
}
sync_substrate_dirichlet_activation(substrate_index);
}
void Microenvironment::unfix_substrates_at_voxel( int voxel_index )
{
for( unsigned int i=0 ; i < dirichlet_activation_vectors[voxel_index].size() ; i++ )
{
dirichlet_activation_vectors[voxel_index][i] = false;
sync_substrate_dirichlet_activation(i);
}
mesh.voxels[voxel_index].is_Dirichlet = false;
return;
}
void Microenvironment::sync_substrate_dirichlet_activation( int substrate_index )
{
for (auto voxel_activation_vector : dirichlet_activation_vectors)
{
if (voxel_activation_vector[substrate_index])
{
dirichlet_activation_vector[substrate_index] = true;
return;
}
}
dirichlet_activation_vector[substrate_index] = false;
return;
}
void Microenvironment::add_dirichlet_node( int voxel_index, std::vector<double>& value )
{
mesh.voxels[voxel_index].is_Dirichlet=true;
dirichlet_value_vectors[voxel_index] = value;
return;
}
void Microenvironment::update_dirichlet_node( int voxel_index , std::vector<double>& new_value )
{
mesh.voxels[voxel_index].is_Dirichlet = true;
dirichlet_value_vectors[voxel_index] = new_value;
return;
}
void Microenvironment::update_dirichlet_node( int voxel_index , int substrate_index , double new_value )
{
mesh.voxels[voxel_index].is_Dirichlet = true;
dirichlet_value_vectors[voxel_index][substrate_index] = new_value;
dirichlet_activation_vectors[voxel_index][substrate_index] = true;
return;
}
void Microenvironment::remove_dirichlet_node( int voxel_index )
{
mesh.voxels[voxel_index].is_Dirichlet = false;
return;
}
bool& Microenvironment::is_dirichlet_node( int voxel_index )
{
return mesh.voxels[voxel_index].is_Dirichlet;
}
void Microenvironment::set_substrate_dirichlet_activation( int substrate_index , bool new_value )
{
dirichlet_activation_vector[substrate_index] = new_value;
for( int n = 0 ; n < mesh.voxels.size() ; n++ )
{ dirichlet_activation_vectors[n][substrate_index] = new_value; }
return;
}
void Microenvironment::set_substrate_dirichlet_activation( int index, std::vector<bool>& new_value )
{
dirichlet_activation_vectors[index] = new_value;
return;
}
bool Microenvironment::get_substrate_dirichlet_activation( int substrate_index )
{
return dirichlet_activation_vector[substrate_index];
}
// TODO? fix confusing swapped usage of args
double Microenvironment::get_substrate_dirichlet_value( int substrate_index, int index )
{
return dirichlet_value_vectors[index][substrate_index];
}
// new functions for finer-grained control of Dirichlet conditions -- 1.7.0
void Microenvironment::set_substrate_dirichlet_activation( int substrate_index , int index, bool new_value )
{
dirichlet_activation_vectors[index][substrate_index] = new_value;
return;
}
bool Microenvironment::get_substrate_dirichlet_activation( int substrate_index, int index )
{ return dirichlet_activation_vectors[index][substrate_index]; }
void Microenvironment::apply_dirichlet_conditions( void )
{
/*
#pragma omp parallel for
for( unsigned int i=0 ; i < dirichlet_indices.size() ; i++ )
{ density_vector( dirichlet_indices[i] ) = dirichlet_value_vectors[i]; }
*/
// #pragma omp parallel for
for( unsigned int i=0 ; i < mesh.voxels.size() ;i++ )
{
/*
if( mesh.voxels[i].is_Dirichlet == true )
{ density_vector(i) = dirichlet_value_vectors[i]; }
*/
if( mesh.voxels[i].is_Dirichlet == true )
{
for( unsigned int j=0; j < dirichlet_value_vectors[i].size(); j++ )
{
// if( dirichlet_activation_vector[j] == true )
if( dirichlet_activation_vectors[i][j] == true )
{
density_vector(i)[j] = dirichlet_value_vectors[i][j];
}
}
}
}
return;
}
void Microenvironment::resize_voxels( int new_number_of_voxes )
{
if( mesh.Cartesian_mesh == true )
{
std::cout << "Error: only use Microenvironment::" << __FUNCTION__ << " as a fall-back for non-Cartesian meshes." << std::endl
<< "\tUse one of the Microenvironment::resize_space() functions instead. Ignoring directive." << std::endl;
return;
}
mesh.voxels.resize( new_number_of_voxes );
temporary_density_vectors1.resize( mesh.voxels.size() , zero );
temporary_density_vectors2.resize( mesh.voxels.size() , zero );
gradient_vectors.resize( mesh.voxels.size() );
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( number_of_densities() );
for( unsigned int i=0 ; i < number_of_densities() ; i++ )
{
(gradient_vectors[k])[i].resize( 3, 0.0 );
}
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vectors.assign( mesh.voxels.size() , dirichlet_activation_vector );
return;
}
void Microenvironment::resize_space( int x_nodes, int y_nodes, int z_nodes )
{
mesh.resize( x_nodes, y_nodes , z_nodes );
temporary_density_vectors1.assign( mesh.voxels.size() , zero );
temporary_density_vectors2.assign( mesh.voxels.size() , zero );
gradient_vectors.resize( mesh.voxels.size() );
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( number_of_densities() );
for( unsigned int i=0 ; i < number_of_densities() ; i++ )
{
(gradient_vectors[k])[i].resize( 3, 0.0 );
}
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vectors.assign( mesh.voxels.size() , dirichlet_activation_vector );
return;
}
void Microenvironment::resize_space( double x_start, double x_end, double y_start, double y_end, double z_start, double z_end , int x_nodes, int y_nodes, int z_nodes )
{
mesh.resize( x_start, x_end, y_start, y_end, z_start, z_end, x_nodes, y_nodes , z_nodes );
temporary_density_vectors1.assign( mesh.voxels.size() , zero );
temporary_density_vectors2.assign( mesh.voxels.size() , zero );
gradient_vectors.resize( mesh.voxels.size() );
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( number_of_densities() );
for( unsigned int i=0 ; i < number_of_densities() ; i++ )
{
(gradient_vectors[k])[i].resize( 3, 0.0 );
}
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vectors.assign( mesh.voxels.size() , dirichlet_activation_vector );
return;
}
void Microenvironment::resize_space( double x_start, double x_end, double y_start, double y_end, double z_start, double z_end , double dx_new , double dy_new , double dz_new )
{
mesh.resize( x_start, x_end, y_start, y_end, z_start, z_end, dx_new , dy_new , dz_new );
temporary_density_vectors1.assign( mesh.voxels.size() , zero );
temporary_density_vectors2.assign( mesh.voxels.size() , zero );
gradient_vectors.resize( mesh.voxels.size() );
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( number_of_densities() );
for( unsigned int i=0 ; i < number_of_densities() ; i++ )
{
(gradient_vectors[k])[i].resize( 3, 0.0 );
}
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vectors.assign( mesh.voxels.size() , dirichlet_activation_vector );
return;
}
void Microenvironment::resize_space_uniform( double x_start, double x_end, double y_start, double y_end, double z_start, double z_end , double dx_new )
{
return resize_space( x_start, x_end, y_start, y_end, z_start, z_end , dx_new , dx_new, dx_new );
}
void Microenvironment::resize_densities( int new_size )
{
zero.assign( new_size, 0.0 );
one.assign( new_size , 1.0 );
temporary_density_vectors1.assign( mesh.voxels.size() , zero );
temporary_density_vectors2.assign( mesh.voxels.size() , zero );
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( number_of_densities() );
for( unsigned int i=0 ; i < number_of_densities() ; i++ )
{
(gradient_vectors[k])[i].resize( 3, 0.0 );
}
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
diffusion_coefficients.assign( new_size , 0.0 );
decay_rates.assign( new_size , 0.0 );
density_names.assign( new_size, "unnamed" );
density_units.assign( new_size , "none" );
one_half = one;
one_half *= 0.5;
one_third = one;
one_third /= 3.0;
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vector.assign( new_size, false );
dirichlet_activation_vectors.assign( mesh.voxels.size(), dirichlet_activation_vector );
default_microenvironment_options.Dirichlet_condition_vector.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_activation_vector.assign( new_size, false );
default_microenvironment_options.initial_condition_vector.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_all.assign( new_size , true );
// default_microenvironment_options.Dirichlet_interior.assign( new_size, true );
default_microenvironment_options.Dirichlet_xmin.assign( new_size , false );
default_microenvironment_options.Dirichlet_xmax.assign( new_size , false );
default_microenvironment_options.Dirichlet_ymin.assign( new_size , false );
default_microenvironment_options.Dirichlet_ymax.assign( new_size , false );
default_microenvironment_options.Dirichlet_zmin.assign( new_size , false );
default_microenvironment_options.Dirichlet_zmax.assign( new_size , false );
default_microenvironment_options.Dirichlet_xmin_values.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_xmax_values.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_ymin_values.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_ymax_values.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_zmin_values.assign( new_size , 1.0 );
default_microenvironment_options.Dirichlet_zmax_values.assign( new_size , 1.0 );
return;
}
void Microenvironment::add_density( void )
{
// fix in PhysiCell preview November 2017
// default_microenvironment_options.use_oxygen_as_first_field = false;
return add_density( "unnamed" , "none" );
}
void Microenvironment::add_density( std::string name , std::string units )
{
// fix in PhysiCell preview November 2017
// default_microenvironment_options.use_oxygen_as_first_field = false;
return add_density( name , units , 0.0 , 0.0 );
}
void Microenvironment::add_density( std::string name , std::string units, double diffusion_constant, double decay_rate )
{
// check if density exist
if ( find_density_index( name ) != -1 )
{
std::cout << "ERROR: density named " << name << " already exists. You probably want your substrates all have unique names!" << std::endl;
exit(-1);
}
// update 1, 0
zero.push_back( 0.0 );
one.push_back( 1.0 );
// update units
density_names.push_back( name );
density_units.push_back( units );
// update coefficients
diffusion_coefficients.push_back( diffusion_constant );
decay_rates.push_back( decay_rate );
// update sources and such
for( unsigned int i=0; i < temporary_density_vectors1.size() ; i++ )
{
temporary_density_vectors1[i].push_back( 0.0 );
temporary_density_vectors2[i].push_back( 0.0 );
}
// resize the gradient data structures
for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ )
{
gradient_vectors[k].resize( number_of_densities() );
for( unsigned int i=0 ; i < number_of_densities() ; i++ )
{
(gradient_vectors[k])[i].resize( 3, 0.0 );
}
}
gradient_vector_computed.resize( mesh.voxels.size() , false );
one_half = one;
one_half *= 0.5;
one_third = one;
one_third /= 3.0;
dirichlet_value_vectors.assign( mesh.voxels.size(), one );
dirichlet_activation_vector.push_back( false );
dirichlet_activation_vectors.assign( mesh.voxels.size(), dirichlet_activation_vector );
// fix in PhysiCell preview November 2017
default_microenvironment_options.Dirichlet_condition_vector.push_back( 1.0 ); // = one;
default_microenvironment_options.Dirichlet_activation_vector.push_back( false ); // assign( number_of_densities(), false );
default_microenvironment_options.initial_condition_vector.push_back( 1.0 );
default_microenvironment_options.Dirichlet_all.push_back( true );
// default_microenvironment_options.Dirichlet_interior.push_back( true );
default_microenvironment_options.Dirichlet_xmin.push_back( false );
default_microenvironment_options.Dirichlet_xmax.push_back( false );
default_microenvironment_options.Dirichlet_ymin.push_back( false );
default_microenvironment_options.Dirichlet_ymax.push_back( false );
default_microenvironment_options.Dirichlet_zmin.push_back( false );
default_microenvironment_options.Dirichlet_zmax.push_back( false );
default_microenvironment_options.Dirichlet_xmin_values.push_back( 1.0 );
default_microenvironment_options.Dirichlet_xmax_values.push_back( 1.0 );
default_microenvironment_options.Dirichlet_ymin_values.push_back( 1.0 );
default_microenvironment_options.Dirichlet_ymax_values.push_back( 1.0 );
default_microenvironment_options.Dirichlet_zmin_values.push_back( 1.0 );
default_microenvironment_options.Dirichlet_zmax_values.push_back( 1.0 );
return;
}
int Microenvironment::find_density_index( std::string name )
{
for( unsigned int i=0; i < density_names.size() ; i++ )
{
if( density_names[i] == name )
{ return i; }
}
return -1;
}
int Microenvironment::find_existing_density_index( std::string name )
{
int i = find_density_index( name );
if( i != -1 )
{ return i; }
std::cerr << "Error: density named " << name << " not found." << std::endl;
exit(-1);
}
void Microenvironment::set_density( int index , std::string name , std::string units )
{
// fix in PhysiCell preview November 2017
if( index == 0 )
{ default_microenvironment_options.use_oxygen_as_first_field = false; }
density_names[index] = name;
density_units[index] = units;
return;
}
void Microenvironment::set_density( int index , std::string name , std::string units , double diffusion_constant , double decay_rate )
{
// fix in PhysiCell preview November 2017
if( index == 0 )
{ default_microenvironment_options.use_oxygen_as_first_field = false; }
density_names[index] = name;
density_units[index] = units;
diffusion_coefficients[index] = diffusion_constant;
decay_rates[index] = decay_rate;
return;
}
int Microenvironment::voxel_index( int i, int j, int k )
{ return mesh.voxel_index(i,j,k) ; }
std::vector<unsigned int> Microenvironment::cartesian_indices( int n )
{ return mesh.cartesian_indices( n ); }
int Microenvironment::nearest_voxel_index( std::vector<double>& position )
{ return mesh.nearest_voxel_index( position ); }
Voxel& Microenvironment::voxels( int voxel_index )
{ return mesh.voxels[voxel_index]; }
std::vector<unsigned int> Microenvironment::nearest_cartesian_indices( std::vector<double>& position )
{ return mesh.nearest_cartesian_indices( position ); }
Voxel& Microenvironment::nearest_voxel( std::vector<double>& position )
{ return mesh.nearest_voxel( position ); }
std::vector<double>& Microenvironment::nearest_density_vector( std::vector<double>& position )
{ return (*p_density_vectors)[ mesh.nearest_voxel_index( position ) ]; }
std::vector<double>& Microenvironment::nearest_density_vector( int voxel_index )
{ return (*p_density_vectors)[ voxel_index ]; }
std::vector<double>& Microenvironment::operator()( int i, int j, int k )
{ return (*p_density_vectors)[ voxel_index(i,j,k) ]; }
std::vector<double>& Microenvironment::operator()( int i, int j )
{ return (*p_density_vectors)[ voxel_index(i,j,0) ]; }
std::vector<double>& Microenvironment::operator()( int n )
{ return (*p_density_vectors)[ n ]; }
std::vector<double>& Microenvironment::density_vector( int i, int j, int k )
{ return (*p_density_vectors)[ voxel_index(i,j,k) ]; }
std::vector<double>& Microenvironment::density_vector( int i, int j )
{ return (*p_density_vectors)[ voxel_index(i,j,0) ]; }
std::vector<double>& Microenvironment::density_vector( int n )
{ return (*p_density_vectors)[ n ]; }
void Microenvironment::simulate_diffusion_decay( double dt )
{
if( diffusion_decay_solver )
{ diffusion_decay_solver( *this, dt ); }
else
{
std::cout << "Warning: diffusion-reaction-source/sink solver not set for Microenvironment object at " << this << ". Nothing happened!" << std::endl;
std::cout << " Consider using Microenvironment::auto_choose_diffusion_decay_solver(void) ... " << std::endl
<< std::endl;
}
return;
}
void Microenvironment::auto_choose_diffusion_decay_solver( void )
{
// set the safest choice
diffusion_decay_solver = diffusion_decay_solver__constant_coefficients_explicit;
std::cout << "Warning: auto-selection of diffusion-decay-source/sink solver not fully implemented!" << std::endl;
// eventual logic: if non-Cartesian, use explicit
// if Cartesian, if non-variable, use the constant coefficient super-fast code
// otherwise, use the variable coefficient code
}
void Microenvironment::display_information( std::ostream& os )
{
os << std::endl << "Microenvironment summary: " << name << ": " << std::endl;
mesh.display_information( os );
os << "Densities: (" << number_of_densities() << " total)" << std::endl;
for( unsigned int i = 0 ; i < density_names.size() ; i++ )
{
os << " " << density_names[i] << ":" << std::endl
<< " units: " << density_units[i] << std::endl
<< " diffusion coefficient: " << diffusion_coefficients[i]
<< " " << spatial_units << "^2 / " << time_units << std::endl
<< " decay rate: " << decay_rates[i]
<< " " << time_units << "^-1" << std::endl
<< " diffusion length scale: " << sqrt( diffusion_coefficients[i] / ( 1e-12 + decay_rates[i] ) )
<< " " << spatial_units << std::endl
<< " initial condition: " << default_microenvironment_options.initial_condition_vector[i]
<< " " << density_units[i] << std::endl
<< " boundary condition: " << default_microenvironment_options.Dirichlet_condition_vector[i]
<< " " << density_units[i] << " (enabled: ";
if( dirichlet_activation_vector[i] == true )
{ os << "true"; }
else
{ os << "false"; }
os << ")" << std::endl;
}
os << std::endl;
return;
}
unsigned int Microenvironment::number_of_densities( void )
{ return (*p_density_vectors)[0].size(); }
unsigned int Microenvironment::number_of_voxels( void )
{ return mesh.voxels.size(); }
unsigned int Microenvironment::number_of_voxel_faces( void )
{ return mesh.voxel_faces.size(); }
void Microenvironment::write_to_matlab( std::string filename )
{
int number_of_data_entries = mesh.voxels.size();
int size_of_each_datum = 3 + 1 + (*p_density_vectors)[0].size();
FILE* fp = write_matlab_header( size_of_each_datum, number_of_data_entries, filename, "multiscale_microenvironment" );
// storing data as cols
for( int i=0; i < number_of_data_entries ; i++ )
{
fwrite( (char*) &( mesh.voxels[i].center[0] ) , sizeof(double) , 1 , fp );
fwrite( (char*) &( mesh.voxels[i].center[1] ) , sizeof(double) , 1 , fp );
fwrite( (char*) &( mesh.voxels[i].center[2] ) , sizeof(double) , 1 , fp );
fwrite( (char*) &( mesh.voxels[i].volume ) , sizeof(double) , 1 , fp );
// densities
for( unsigned int j=0 ; j < (*p_density_vectors)[i].size() ; j++)
{ fwrite( (char*) &( ((*p_density_vectors)[i])[j] ) , sizeof(double) , 1 , fp ); }
}
fclose( fp );
return;
}
void Microenvironment::simulate_bulk_sources_and_sinks( double dt )
{
if( !bulk_source_sink_solver_setup_done )
{
bulk_source_sink_solver_temp1.resize( mesh.voxels.size() , zero );
bulk_source_sink_solver_temp2.resize( mesh.voxels.size() , zero );
bulk_source_sink_solver_temp3.resize( mesh.voxels.size() , zero );
bulk_source_sink_solver_setup_done = true;
}
#pragma omp parallel for
for( unsigned int i=0; i < mesh.voxels.size() ; i++ )
{
bulk_supply_rate_function( this,i, &bulk_source_sink_solver_temp1[i] ); // temp1 = S
bulk_supply_target_densities_function( this,i, &bulk_source_sink_solver_temp2[i]); // temp2 = T
bulk_uptake_rate_function( this,i, &bulk_source_sink_solver_temp3[i] ); // temp3 = U
bulk_source_sink_solver_temp2[i] *= bulk_source_sink_solver_temp1[i]; // temp2 = S*T
axpy( &(*p_density_vectors)[i] , dt , bulk_source_sink_solver_temp2[i] ); // out = out + dt*temp2 = out + dt*S*T
bulk_source_sink_solver_temp3[i] += bulk_source_sink_solver_temp1[i]; // temp3 = U+S
bulk_source_sink_solver_temp3[i] *= dt; // temp3 = dt*(U+S)
bulk_source_sink_solver_temp3[i] += one; // temp3 = 1 + dt*(U+S)
(*p_density_vectors)[i] /= bulk_source_sink_solver_temp3[i];
}
return;
}
void Microenvironment::simulate_cell_sources_and_sinks( std::vector<Basic_Agent*>& basic_agent_list , double dt )
{
#pragma omp parallel for
for( unsigned int i=0 ; i < basic_agent_list.size() ; i++ )
{
basic_agent_list[i]->simulate_secretion_and_uptake( this , dt );
}
return;
}
void Microenvironment::simulate_cell_sources_and_sinks( double dt )
{
simulate_cell_sources_and_sinks(all_basic_agents, dt);
}
void Microenvironment::update_rates( void )
{
if( supply_target_densities_times_supply_rates.size() != number_of_voxels() )
{ supply_target_densities_times_supply_rates.assign( number_of_voxels() , zero ); }
if( supply_rates.size() != number_of_voxels() )
{ supply_rates.assign( number_of_voxels() , zero ); }
if( uptake_rates.size() != number_of_voxels() )
{ uptake_rates.assign( number_of_voxels() , zero ); }
#pragma omp parallel for
for( unsigned int i=0 ; i < number_of_voxels() ; i++ )
{
bulk_uptake_rate_function( this,i, &(uptake_rates[i]) );
bulk_supply_rate_function( this,i, &(supply_rates[i]) );
bulk_supply_target_densities_function( this,i, &(supply_target_densities_times_supply_rates[i]) );
supply_target_densities_times_supply_rates[i] *= supply_rates[i];
}
return;
}
std::vector<gradient>& Microenvironment::gradient_vector(int i, int j, int k)
{
int n = voxel_index(i,j,k);
if( gradient_vector_computed[n] == false )
{
compute_gradient_vector( n );
}
return gradient_vectors[n];
}
std::vector<gradient>& Microenvironment::gradient_vector(int i, int j )
{
int n = voxel_index(i,j,0);
if( gradient_vector_computed[n] == false )
{
compute_gradient_vector( n );
}
return gradient_vectors[n];
}
std::vector<gradient>& Microenvironment::gradient_vector(int n )
{
// if the gradient has not yet been computed, then do it!
if( gradient_vector_computed[n] == false )
{
compute_gradient_vector( n );
}