Trisurf Monte Carlo simulator
Samo Penic
2014-02-11 6aec63272fd6fe35a6ded508c2f2e3e19e107215
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
SP 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     }
bb77ca 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     }
bb77ca 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
d7639a 72 ts_bool cell_add_vertex(ts_cell *cell, ts_vertex *vtx){
34d3de 73     cell->nvertex++;
SP 74     cell->vertex=(ts_vertex **)realloc(cell->vertex,cell->nvertex*sizeof(ts_vertex *));
75         if(cell->vertex == NULL){
b01cc1 76             fatal("Reallocation of memory failed during insertion of vertex in cell_add_vertex",3);
d7639a 77         }
34d3de 78     cell->vertex[cell->nvertex-1]=vtx;
d7639a 79     return TS_SUCCESS;
SP 80 }
81
bb77ca 82
SP 83 ts_bool cell_list_cell_occupation_clear(ts_cell_list *clist){
d7639a 84     ts_uint i;
SP 85     for(i=0;i<clist->cellno;i++){
34d3de 86         if(clist->cell[i]->vertex != NULL){
SP 87             free(clist->cell[i]->vertex);
88             clist->cell[i]->vertex=NULL;
d7639a 89         }
34d3de 90         clist->cell[i]->nvertex=0;
d7639a 91     }
SP 92     return TS_SUCCESS;
93 }
94
bb77ca 95 // TODO: compiles ok, but it is completely untested and undebugged. It was debugged before rewrite, but this was long time ago.
d7639a 96 ts_bool cell_occupation_number_and_internal_proximity(ts_cell_list *clist, ts_uint cellidx, ts_vertex *vtx, ts_vertex *tvtx){
SP 97     ts_uint ncx,ncy,ncz,remainder,cell_occupation;
bb77ca 98     ts_uint i,j,k,l,neigh_cidx;
d7639a 99     ts_double dist;
bb77ca 100     ncx=(cellidx+1)/(clist->ncmax[2]*clist->ncmax[1])+1; //+1 because of zero indexing.
d7639a 101     remainder=(cellidx+1)%(clist->ncmax[2]*clist->ncmax[1]);
SP 102     ncy=remainder/clist->ncmax[2]+1;
103     ncz=remainder%clist->ncmax[2];
104 //    fprintf(stderr,"here are ncx,ncy,ncz=%i,%i,%i\n",ncx,ncy,ncz);
105
106     for(i=ncx-1;i<=ncx+1;i++){
107         for(j=ncy-1;j<=ncy+1;j++){
108             for(k=ncz-1;k<=ncz+1;k++){
109                 neigh_cidx=k+(j-1)*clist->ncmax[2]+(i-1)*clist->ncmax[2]*clist->ncmax[1] -1;
110           //      fprintf(stderr,"neigh_cell_index=%i\n",neigh_cidx);
34d3de 111                 cell_occupation=clist->cell[neigh_cidx]->nvertex;
d7639a 112           //      fprintf(stderr, "cell_occupation=%i\n",cell_occupation);
SP 113                 if(cell_occupation>clist->max_occupancy){
114                     fatal("Neighbouring cell occupation more than set max_occupancy value.",2000);
115                 }
116 // Now we check whether we didn't come close to some other vertices in the same
117 // cell!
118                 if(cell_occupation>1){
119                     for(l=0;l<cell_occupation;l++){
34d3de 120                         if(clist->cell[neigh_cidx]->vertex[l]!=vtx){
d7639a 121                     //        fprintf(stderr,"calling dist on vertex %i\n",l);
34d3de 122                            dist=vtx_distance_sq(clist->cell[neigh_cidx]->vertex[l],tvtx);
d7639a 123                     //        fprintf(stderr,"dist was %f\n",dist);
SP 124                             if(dist<1) return TS_FAIL;
125                         }
126                     }
127                 }
128             }
129         }
130     }
131     
132     return TS_SUCCESS;
133 }