Trisurf Monte Carlo simulator
Samo Penic
2016-02-15 82fa4172eadaff3e8c5f456ceefe947fb2fcec4c
commit | author | age
d7639a 1 #include<stdlib.h>
SP 2 #include "general.h"
3 #include "vertex.h"
bb77ca 4
SP 5 ts_cell_list  *init_cell_list(ts_uint ncmax1, ts_uint ncmax2, ts_uint ncmax3, ts_double stepsize){
d7639a 6     ts_uint i;
bb77ca 7     ts_uint nocells=ncmax1*ncmax2*ncmax3;
SP 8     ts_cell_list *clist=(ts_cell_list *)malloc(sizeof(ts_cell_list));
9     if(clist==NULL) fatal("Error while allocating memory for cell list!",100);
10
11     clist->ncmax[0]=ncmax1;
12     clist->ncmax[1]=ncmax2;
13     clist->ncmax[2]=ncmax3;
14     clist->cellno=nocells;
d7639a 15     clist->dcell=1.0/(1.0 + stepsize);
SP 16     clist->shift=(ts_double) clist->ncmax[0]/2;
bb77ca 17
SP 18     clist->cell=(ts_cell **)malloc(nocells*sizeof(ts_cell *));
19     if(clist->cell==NULL) fatal("Error while allocating memory for cell list! ncmax too large?",101);
20
d7639a 21     for(i=0;i<nocells;i++){
34d3de 22         clist->cell[i]=(ts_cell *)calloc(1,sizeof(ts_cell));
bb77ca 23         if(clist->cell[i]==NULL) fatal("Error while allocating memory for cell list! ncmax too large?",102);
SP 24         clist->cell[i]->idx=i+1; // We enumerate cells! Probably never required!
d7639a 25     }
bb77ca 26     return clist;
SP 27 }
28
29 ts_bool cell_free(ts_cell* cell){
34d3de 30     if(cell->vertex!=NULL) free(cell->vertex);
bb77ca 31     free(cell);
d7639a 32     return TS_SUCCESS;
SP 33 }
34
35 ts_bool cell_list_free(ts_cell_list *clist){
36     ts_uint i;
bb77ca 37     if(clist==NULL) return TS_FAIL;
SP 38     ts_uint nocells=clist->cellno;
d7639a 39     for(i=0;i<nocells;i++)
bb77ca 40          if(clist->cell[i] != NULL) cell_free(clist->cell[i]);
d7639a 41     free(clist->cell);
bb77ca 42     free(clist);
d7639a 43     return TS_SUCCESS;
SP 44 }
45
46
bb77ca 47 inline ts_uint vertex_self_avoidance(ts_vesicle *vesicle, ts_vertex *vtx){
SP 48     ts_uint cellidx;
49     ts_uint ncx, ncy,ncz;
50     ts_cell_list *clist=vesicle->clist;
8f6a69 51     ncx=(ts_uint)((vtx->x-vesicle->cm[0])*clist->dcell+clist->shift);
SP 52     ncy=(ts_uint)((vtx->y-vesicle->cm[1])*clist->dcell+clist->shift);
53     ncz=(ts_uint)((vtx->z-vesicle->cm[2])*clist->dcell+clist->shift);
bb77ca 54
a63f17 55     if(ncx >= clist->ncmax[0]-1 || ncx <= 2){
d7639a 56         fatal("Vesicle is positioned outside the cell covered area. Coordinate x is the problem.",1500);
SP 57     }
a63f17 58     if(ncy >= clist->ncmax[1]-1 || ncy <= 2){
d7639a 59         fatal("Vesicle is positioned outside the cell covered area. Coordinate y is the problem.",1500);
SP 60     }
a63f17 61     if(ncz >= clist->ncmax[2]-1 || ncz <= 2){
d7639a 62         fatal("Vesicle is positioned outside the cell covered area. Coordinate z is the problem.",1500);
SP 63     }
bb77ca 64     cellidx=ncz+(ncy-1)*clist->ncmax[2] + (ncx-1)*clist->ncmax[2]* 
SP 65                                     clist->ncmax[1] - 1; // -1 is because of 0 based indexing
66         return cellidx;
d7639a 67 }
SP 68
69
a63f17 70 inline ts_bool cell_add_vertex(ts_cell *cell, ts_vertex *vtx){
SP 71     ts_uint i;
72     for(i=0;i<cell->nvertex;i++){
73         if(cell->vertex[i]==vtx){
74     //vertex is already in the cell!
75             //fprintf(stderr,"VTX in the cell!\n");
76             return TS_FAIL;
77         }
78     }
79             //fprintf(stderr,"VTX added to the cell!\n");
34d3de 80     cell->nvertex++;
SP 81     cell->vertex=(ts_vertex **)realloc(cell->vertex,cell->nvertex*sizeof(ts_vertex *));
82         if(cell->vertex == NULL){
b01cc1 83             fatal("Reallocation of memory failed during insertion of vertex in cell_add_vertex",3);
d7639a 84         }
34d3de 85     cell->vertex[cell->nvertex-1]=vtx;
a63f17 86     vtx->cell=cell;
d7639a 87     return TS_SUCCESS;
SP 88 }
89
a63f17 90 inline ts_bool cell_remove_vertex(ts_cell *cell, ts_vertex *vtx){
SP 91    ts_uint i,j=0;
92     for(i=0;i<cell->nvertex;i++){
93         if(cell->vertex[i]!=vtx){
94             cell->vertex[j]=cell->vertex[i];
95             j++;
96         }
97     }
98     if(j==i){
99     fatal("Vertex was not in the cell!",3);
100     } 
101     //fprintf(stderr, "Vertex deleted from the cell!\n");
102
103 /* resize memory. potentionally time consuming */
104     cell->nvertex--;
105     cell->vertex=(ts_vertex **)realloc(cell->vertex,cell->nvertex*sizeof(ts_vertex *));
106     if(vtx->neigh == NULL && vtx->neigh_no!=0)
107         if(cell->vertex == NULL){
108             fatal("Reallocation of memory failed during removal of vertex in cell_remove_vertex",3);
109         }
110     return TS_SUCCESS;
111 }
bb77ca 112
SP 113 ts_bool cell_list_cell_occupation_clear(ts_cell_list *clist){
d7639a 114     ts_uint i;
SP 115     for(i=0;i<clist->cellno;i++){
34d3de 116         if(clist->cell[i]->vertex != NULL){
SP 117             free(clist->cell[i]->vertex);
118             clist->cell[i]->vertex=NULL;
d7639a 119         }
34d3de 120         clist->cell[i]->nvertex=0;
d7639a 121     }
SP 122     return TS_SUCCESS;
123 }
124
ea1cce 125
a63f17 126 ts_bool cell_occupation_number_and_internal_proximity(ts_cell_list *clist, ts_uint cellidx, ts_vertex *vtx){
d7639a 127     ts_uint ncx,ncy,ncz,remainder,cell_occupation;
bb77ca 128     ts_uint i,j,k,l,neigh_cidx;
d7639a 129     ts_double dist;
bb77ca 130     ncx=(cellidx+1)/(clist->ncmax[2]*clist->ncmax[1])+1; //+1 because of zero indexing.
d7639a 131     remainder=(cellidx+1)%(clist->ncmax[2]*clist->ncmax[1]);
SP 132     ncy=remainder/clist->ncmax[2]+1;
133     ncz=remainder%clist->ncmax[2];
134 //    fprintf(stderr,"here are ncx,ncy,ncz=%i,%i,%i\n",ncx,ncy,ncz);
135
136     for(i=ncx-1;i<=ncx+1;i++){
137         for(j=ncy-1;j<=ncy+1;j++){
138             for(k=ncz-1;k<=ncz+1;k++){
139                 neigh_cidx=k+(j-1)*clist->ncmax[2]+(i-1)*clist->ncmax[2]*clist->ncmax[1] -1;
140           //      fprintf(stderr,"neigh_cell_index=%i\n",neigh_cidx);
34d3de 141                 cell_occupation=clist->cell[neigh_cidx]->nvertex;
d7639a 142           //      fprintf(stderr, "cell_occupation=%i\n",cell_occupation);
SP 143                 if(cell_occupation>clist->max_occupancy){
144                     fatal("Neighbouring cell occupation more than set max_occupancy value.",2000);
145                 }
146 // Now we check whether we didn't come close to some other vertices in the same
147 // cell!
a18ed8 148                 if(cell_occupation>0){
d7639a 149                     for(l=0;l<cell_occupation;l++){
a63f17 150
SP 151                 //carefull with this checks!
555fd8 152                         if(clist->cell[neigh_cidx]->vertex[l]!=vtx){
d7639a 153                     //        fprintf(stderr,"calling dist on vertex %i\n",l);
a63f17 154                            dist=vtx_distance_sq(clist->cell[neigh_cidx]->vertex[l],vtx);
555fd8 155
M 156 //                if(vtx->idx==1)
157 //                fprintf(stderr,"VTX(0) ima bliznji vertex z indeksom, %d, tipa %d \n", clist->cell[neigh_cidx]->vertex[l]->idx, clist->cell[neigh_cidx]->vertex[l]->id);
158 //                if(vtx->idx==0 && clist->cell[neigh_cidx]->vertex[l]->idx==0)
159 //                            fprintf(stderr,"*** dist was %f\n",dist);
160                 
ea1cce 161                             if(dist<=1.0 || (dist<=clist->dmin_interspecies && (clist->cell[neigh_cidx]->vertex[l]->id != vtx->id))) return TS_FAIL;
d7639a 162                         }
SP 163                     }
164                 }
165             }
166         }
a63f17 167     } 
d7639a 168     return TS_SUCCESS;
SP 169 }