Trisurf Monte Carlo simulator
Samo Penic
2012-07-13 672ae43d88ba88cf1f4183511bd56f90c7c4d2ed
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 //TODO: not debugged at all!
SP 48 inline ts_uint vertex_self_avoidance(ts_vesicle *vesicle, ts_vertex *vtx){
49     ts_uint cellidx;
50     ts_uint ncx, ncy,ncz;
51     ts_cell_list *clist=vesicle->clist;
8f6a69 52     ncx=(ts_uint)((vtx->x-vesicle->cm[0])*clist->dcell+clist->shift);
SP 53     ncy=(ts_uint)((vtx->y-vesicle->cm[1])*clist->dcell+clist->shift);
54     ncz=(ts_uint)((vtx->z-vesicle->cm[2])*clist->dcell+clist->shift);
bb77ca 55
a63f17 56     if(ncx >= clist->ncmax[0]-1 || ncx <= 2){
d7639a 57         fatal("Vesicle is positioned outside the cell covered area. Coordinate x is the problem.",1500);
SP 58     }
a63f17 59     if(ncy >= clist->ncmax[1]-1 || ncy <= 2){
d7639a 60         fatal("Vesicle is positioned outside the cell covered area. Coordinate y is the problem.",1500);
SP 61     }
a63f17 62     if(ncz >= clist->ncmax[2]-1 || ncz <= 2){
d7639a 63         fatal("Vesicle is positioned outside the cell covered area. Coordinate z is the problem.",1500);
SP 64     }
bb77ca 65     cellidx=ncz+(ncy-1)*clist->ncmax[2] + (ncx-1)*clist->ncmax[2]* 
SP 66                                     clist->ncmax[1] - 1; // -1 is because of 0 based indexing
67         return cellidx;
d7639a 68 }
SP 69
70
bb77ca 71 //TODO: looks ok, but debug anyway in the future
a63f17 72 inline ts_bool cell_add_vertex(ts_cell *cell, ts_vertex *vtx){
SP 73     ts_uint i;
74     for(i=0;i<cell->nvertex;i++){
75         if(cell->vertex[i]==vtx){
76     //vertex is already in the cell!
77             //fprintf(stderr,"VTX in the cell!\n");
78             return TS_FAIL;
79         }
80     }
81             //fprintf(stderr,"VTX added to the cell!\n");
34d3de 82     cell->nvertex++;
SP 83     cell->vertex=(ts_vertex **)realloc(cell->vertex,cell->nvertex*sizeof(ts_vertex *));
84         if(cell->vertex == NULL){
b01cc1 85             fatal("Reallocation of memory failed during insertion of vertex in cell_add_vertex",3);
d7639a 86         }
34d3de 87     cell->vertex[cell->nvertex-1]=vtx;
a63f17 88     vtx->cell=cell;
d7639a 89     return TS_SUCCESS;
SP 90 }
91
a63f17 92 inline ts_bool cell_remove_vertex(ts_cell *cell, ts_vertex *vtx){
SP 93    ts_uint i,j=0;
94     for(i=0;i<cell->nvertex;i++){
95         if(cell->vertex[i]!=vtx){
96             cell->vertex[j]=cell->vertex[i];
97             j++;
98         }
99     }
100     if(j==i){
101     fatal("Vertex was not in the cell!",3);
102     } 
103     //fprintf(stderr, "Vertex deleted from the cell!\n");
104
105 /* resize memory. potentionally time consuming */
106     cell->nvertex--;
107     cell->vertex=(ts_vertex **)realloc(cell->vertex,cell->nvertex*sizeof(ts_vertex *));
108     if(vtx->neigh == NULL && vtx->neigh_no!=0)
109         if(cell->vertex == NULL){
110             fatal("Reallocation of memory failed during removal of vertex in cell_remove_vertex",3);
111         }
112     return TS_SUCCESS;
113 }
bb77ca 114
SP 115 ts_bool cell_list_cell_occupation_clear(ts_cell_list *clist){
d7639a 116     ts_uint i;
SP 117     for(i=0;i<clist->cellno;i++){
34d3de 118         if(clist->cell[i]->vertex != NULL){
SP 119             free(clist->cell[i]->vertex);
120             clist->cell[i]->vertex=NULL;
d7639a 121         }
34d3de 122         clist->cell[i]->nvertex=0;
d7639a 123     }
SP 124     return TS_SUCCESS;
125 }
126
bb77ca 127 // TODO: compiles ok, but it is completely untested and undebugged. It was debugged before rewrite, but this was long time ago.
a63f17 128 ts_bool cell_occupation_number_and_internal_proximity(ts_cell_list *clist, ts_uint cellidx, ts_vertex *vtx){
d7639a 129     ts_uint ncx,ncy,ncz,remainder,cell_occupation;
bb77ca 130     ts_uint i,j,k,l,neigh_cidx;
d7639a 131     ts_double dist;
bb77ca 132     ncx=(cellidx+1)/(clist->ncmax[2]*clist->ncmax[1])+1; //+1 because of zero indexing.
d7639a 133     remainder=(cellidx+1)%(clist->ncmax[2]*clist->ncmax[1]);
SP 134     ncy=remainder/clist->ncmax[2]+1;
135     ncz=remainder%clist->ncmax[2];
136 //    fprintf(stderr,"here are ncx,ncy,ncz=%i,%i,%i\n",ncx,ncy,ncz);
137
138     for(i=ncx-1;i<=ncx+1;i++){
139         for(j=ncy-1;j<=ncy+1;j++){
140             for(k=ncz-1;k<=ncz+1;k++){
141                 neigh_cidx=k+(j-1)*clist->ncmax[2]+(i-1)*clist->ncmax[2]*clist->ncmax[1] -1;
142           //      fprintf(stderr,"neigh_cell_index=%i\n",neigh_cidx);
34d3de 143                 cell_occupation=clist->cell[neigh_cidx]->nvertex;
d7639a 144           //      fprintf(stderr, "cell_occupation=%i\n",cell_occupation);
SP 145                 if(cell_occupation>clist->max_occupancy){
146                     fatal("Neighbouring cell occupation more than set max_occupancy value.",2000);
147                 }
148 // Now we check whether we didn't come close to some other vertices in the same
149 // cell!
150                 if(cell_occupation>1){
151                     for(l=0;l<cell_occupation;l++){
a63f17 152
SP 153                 //carefull with this checks!
154                         if(clist->cell[neigh_cidx]->vertex[l]->idx!=vtx->idx){
d7639a 155                     //        fprintf(stderr,"calling dist on vertex %i\n",l);
a63f17 156                            dist=vtx_distance_sq(clist->cell[neigh_cidx]->vertex[l],vtx);
d7639a 157                     //        fprintf(stderr,"dist was %f\n",dist);
a63f17 158                             if(dist<=1.0) return TS_FAIL;
d7639a 159                         }
SP 160                     }
161                 }
162             }
163         }
a63f17 164     } 
d7639a 165     return TS_SUCCESS;
SP 166 }