Trisurf Monte Carlo simulator
Samo Penic
2014-09-11 7b0c077e18af45b1312c35aa52efb0211d1378c5
commit | author | age
d7639a 1 #include<stdlib.h>
SP 2 #include<math.h>
3 #include<string.h>
4 #include "general.h"
5 #include "vertex.h"
a10dd5 6 #include "bond.h"
d7639a 7 #include<stdio.h>
SP 8
ea1cce 9 ts_bool vertex_list_assign_id(ts_vertex_list *vlist, ts_uint id){
M 10     ts_uint i;    
11     for(i=0;i<vlist->n;i++){
12         vlist->vtx[i]->id = id;
13     }
14     return TS_SUCCESS;
15 }
16
73f967 17 ts_vertex_list *init_vertex_list(ts_uint N){    
d7639a 18     ts_int i;
a10dd5 19     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
73f967 20     
d7639a 21     if(N==0){
SP 22         err("Initialized vertex list with zero elements. Pointer set to NULL");
73f967 23         vlist->n=0;
SP 24         vlist->vtx=NULL;
25         return vlist;
d7639a 26     }
73f967 27     
8f6a69 28     vlist->vtx=(ts_vertex **)calloc(N,sizeof(ts_vertex *));
SP 29     if(vlist->vtx==NULL)
73f967 30         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
a10dd5 31     for(i=0;i<N;i++) {
8f6a69 32         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
a10dd5 33         vlist->vtx[i]->idx=i;
074a17 34
SP 35     /* initialize Ylm for spherical hamonics DONE in sh.c */
36 /*    for(i=0;i<l;i++){
37         vlist->vtx[i]->Ylm[i]=(ts_double **)calloc(2*i+1,sizeof(ts_double *));
38         for(j=0;j<(2*i+1);j++){
39             clist->vtx[i]->Ylm[i][j]=(ts_double *)calloc(sizeof(ts_double));
40         }
41     }
42 */
43
44
a10dd5 45     }
73f967 46     vlist->n=N;
d7639a 47     return vlist;
SP 48 }
49
737714 50 ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx){
d7639a 51     ts_uint i;
SP 52     /* no neighbour can be null! */
53     if(vtx==NULL || nvtx==NULL) return TS_FAIL;
54     
55     /*if it is already a neighbour don't add it to the list */
8f6a69 56     for(i=0; i<vtx->neigh_no;i++){
SP 57         if(vtx->neigh[i]==nvtx) return TS_FAIL;
d7639a 58     }
8f6a69 59     ts_uint nn=++vtx->neigh_no;
SP 60     vtx->neigh=(ts_vertex **)realloc(vtx->neigh, nn*sizeof(ts_vertex *));
61     vtx->neigh[nn-1]=nvtx;
3131dc 62 /* This was a bug in creating DIPYRAMID (the neighbours were not in right
SP 63  * order).
64  */
d7639a 65     /* pa se sosedu dodamo vertex */
SP 66     /*if it is already a neighbour don't add it to the list */
3131dc 67 /*
737714 68     for(i=0; i<nvtx->data->neigh_no;i++){
SP 69         if(nvtx->data->neigh[i]==vtx) return TS_FAIL;
d7639a 70     } 
a10dd5 71     nn=++nvtx->data->neigh_no;
737714 72     nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *));
a10dd5 73     nvtx->data->neigh[nn-1]=vtx;
3131dc 74 */
d7639a 75
SP 76     return TS_SUCCESS;
77 }
a10dd5 78
9802f1 79 /* TODO: optimize this. test this. */
SP 80 ts_bool vtx_remove_neighbour(ts_vertex *vtx, ts_vertex *nvtx){
81 /* find a neighbour */
82 /* remove it from the list while shifting remaining neighbours up */
e19e79 83     ts_uint i,j=0;
8f6a69 84     for(i=0;i<vtx->neigh_no;i++){
e19e79 85 //        fprintf(stderr,"neigh_addr=%ld\n", (long)vtx->neigh[i]);
8f6a69 86         if(vtx->neigh[i]!=nvtx){
SP 87             vtx->neigh[j]=vtx->neigh[i];
9802f1 88             j++;
SP 89         }
90     }
e19e79 91 //    fprintf(stderr,"remove_neighbour: vtx1_addr=%ld, vtx2_addr=%ld\n",(long)vtx,(long)nvtx);
9802f1 92 /* resize memory. potentionally time consuming */
8f6a69 93     vtx->neigh_no--;
SP 94     vtx->neigh=(ts_vertex **)realloc(vtx->neigh,vtx->neigh_no*sizeof(ts_vertex *));
95     if(vtx->neigh == NULL && vtx->neigh_no!=0)
e19e79 96         fatal("(1) Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100);
SP 97 //fprintf(stderr,"first alloc");
9802f1 98 /* repeat for the neighbour */
SP 99 /* find a neighbour */
100 /* remove it from the list while shifting remaining neighbours up */
e19e79 101     j=0;
8f6a69 102     for(i=0;i<nvtx->neigh_no;i++){
SP 103         if(nvtx->neigh[i]!=vtx){
104             nvtx->neigh[j]=nvtx->neigh[i];
9802f1 105             j++;
SP 106         }
107     }
108 /* resize memory. potentionally time consuming. */
e19e79 109 //    fprintf(stderr,"Neigbours=%d\n",nvtx->neigh_no);
8f6a69 110     nvtx->neigh_no--;
e19e79 111             nvtx->neigh=(ts_vertex **)realloc(nvtx->neigh,nvtx->neigh_no*sizeof(ts_vertex *));
SP 112 //    fprintf(stderr,"Neigbours=%d\n",nvtx->neigh_no);
8f6a69 113     if(nvtx->neigh == NULL && nvtx->neigh_no!=0)
e19e79 114         fatal("(2) Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100);
9802f1 115
SP 116     return TS_SUCCESS;
117 }
7958e9 118
9802f1 119
SP 120
a10dd5 121 ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){
SP 122     ts_bond *bond;
123     bond=bond_add(blist,vtx1,vtx2);
124     if(bond==NULL) return TS_FAIL;
8f6a69 125     vtx1->bond_no++;
30ee9c 126     vtx2->bond_no++;
3131dc 127    // vtx2->data->bond_no++;
a10dd5 128
8f6a69 129     vtx1->bond=(ts_bond **)realloc(vtx1->bond, vtx1->bond_no*sizeof(ts_bond *)); 
30ee9c 130     vtx2->bond=(ts_bond **)realloc(vtx2->bond, vtx2->bond_no*sizeof(ts_bond *)); 
3131dc 131    // vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); 
8f6a69 132     vtx1->bond[vtx1->bond_no-1]=bond;
30ee9c 133     vtx2->bond[vtx2->bond_no-1]=bond;
8f6a69 134    // vtx2->ata->bond[vtx2->data->bond_no-1]=bond;
a10dd5 135     return TS_SUCCESS;
SP 136 }
137
138 ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
139     ts_bool retval;
140     retval=vtx_add_neighbour(vtx1,vtx2);
e19e79 141   //  retval=vtx_add_neighbour(vtx2,vtx1);
a10dd5 142     if(retval==TS_SUCCESS)
SP 143     retval=vtx_add_bond(blist,vtx1,vtx2); 
144     return retval;
145 }
146
9802f1 147 /*TODO: write and optimize this urgently before use! */
SP 148 ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex
149 *vtx2){
150 //    ts_bool retval;
151 /* remove the bond */
152 //retval=vtx_remove_bond(blist,vtx1,vtx2);
153 /* remove the vertices */
154     return TS_SUCCESS;
155 }
a10dd5 156
d7639a 157
737714 158 ts_bool vtx_free(ts_vertex  *vtx){
8f6a69 159     if(vtx->neigh!=NULL)   free(vtx->neigh);
SP 160     if(vtx->tristar!=NULL) free(vtx->tristar);
161     if(vtx->bond!=NULL)    free(vtx->bond);
737714 162     free(vtx);
d7639a 163     return TS_SUCCESS;
SP 164 }
165
73f967 166 ts_bool vtx_list_free(ts_vertex_list *vlist){
SP 167     int i;
168     for(i=0;i<vlist->n;i++){
8f6a69 169         if(vlist->vtx[i]!=NULL) vtx_free(vlist->vtx[i]);
73f967 170     }
8f6a69 171     //free(*(vlist->vtx));
737714 172     free(vlist->vtx);
73f967 173     free(vlist);
SP 174     return TS_SUCCESS;
175 }
d7639a 176
bb77ca 177 inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){
d7639a 178     ts_double dist;
SP 179 #ifdef TS_DOUBLE_DOUBLE
8f6a69 180     dist=pow(vtx1->x-vtx2->x,2) + pow(vtx1->y-vtx2->y,2) + pow(vtx1->z-vtx2->z,2);
d7639a 181 #endif
SP 182 #ifdef TS_DOUBLE_LONGDOUBLE
8f6a69 183     dist=powl(vtx1->x-vtx2->x,2) + powl(vtx1->y-vtx2->y,2) + powl(vtx1->z-vtx2->z,2);
d7639a 184 #endif
SP 185 #ifdef TS_DOUBLE_FLOAT
8f6a69 186     dist=powf(vtx1->x-vtx2->x,2) + powf(vtx1->y-vtx2->y,2) + powf(vtx1->z-vtx2->z,2);
d7639a 187 #endif
SP 188     return(dist);
189 }
bb77ca 190
7958e9 191
SP 192
193 ts_bool vtx_set_global_values(ts_vesicle *vesicle){ 
194     ts_double xk=vesicle->bending_rigidity;
195     ts_uint i; 
196     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 197         vesicle->vlist->vtx[i]->xk=xk;
7958e9 198     }
SP 199     return TS_SUCCESS;
200 }
201
202 inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){
8f6a69 203     ts_double dX2=vtx2->x-vtx1->x;
SP 204     ts_double dY2=vtx2->y-vtx1->y;
205     ts_double dZ2=vtx2->z-vtx1->z;
206     ts_double dX3=vtx3->x-vtx1->x;
207     ts_double dY3=vtx3->y-vtx1->y;
208     ts_double dZ3=vtx3->z-vtx1->z;
209     ts_double direct=vtx1->x*(dY2*dZ3 -dZ2*dY3)+ 
210         vtx1->y*(dZ2*dX3-dX2*dZ3)+
211         vtx1->z*(dX2*dY3-dY2*dX3);
7958e9 212     return(direct);    
SP 213 }
214
215
216 inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem){
8f6a69 217     vtx->tristar_no++;
SP 218     vtx->tristar=(ts_triangle **)realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *));
219     if(vtx->tristar==NULL){
7958e9 220             fatal("Reallocation of memory while adding tristar failed.",3);
SP 221     }
8f6a69 222     vtx->tristar[vtx->tristar_no-1]=tristarmem;
7958e9 223     return TS_SUCCESS;
SP 224 }
225
f74313 226
30ee9c 227 /* Insert neighbour is a function that is required in bondflip. It inserts a
SP 228  * neighbour exactly in the right place. */
229 inline ts_bool vtx_insert_neighbour(ts_vertex *vtx, ts_vertex *nvtx, ts_vertex *vtxm){
230 //nvtx is a vertex that is to be inserted after vtxm!
231         ts_uint i,j,midx;
232         vtx->neigh_no++;
233         if(vtxm==NULL ||  nvtx==NULL || vtx==NULL)
234                 fatal("vertex_insert_neighbour: one of pointers has been zero.. Cannot proceed.",3);
235         //We need to reallocate space! The pointer *neight must be zero if not having neighbours jey (if neigh_no was 0 at thime of calling
236         vtx->neigh=realloc(vtx->neigh,vtx->neigh_no*sizeof(ts_vertex *));
237         if(vtx->neigh == NULL){
238             fatal("Reallocation of memory failed during insertion of vertex neighbour in vertex_insert_neighbour",3);
239         }
240         midx=0;
241         for(i=0;i<vtx->neigh_no-1;i++) if(vtx->neigh[i]==vtxm) {midx=i; break;}
242      //   fprintf(stderr,"midx=%d, vseh=%d\n",midx,vtx->neigh_no-2);
243         if(midx==vtx->neigh_no-2) {
244             vtx->neigh[vtx->neigh_no-1]=nvtx;
245         } else {
246             for(j=vtx->neigh_no-2;j>midx;j--) {
247                 vtx->neigh[j+1]=vtx->neigh[j];
248 //                vtx->bond_length[j+1]=vtx->bond_length[j];
249 //                vtx->bond_length_dual[j+1]=vtx->bond_length_dual[j];
250             }
251             vtx->neigh[midx+1]=nvtx;
252         }
253     return TS_SUCCESS;
254 }
255
256
257 /* vtx remove tristar is required in  bondflip. */
258 /* TODO: Check whether it is important to keep the numbering of tristar
259  * elements in some order or not! */
260 inline ts_bool vtx_remove_tristar(ts_vertex *vtx, ts_triangle *tristar){
261     ts_uint i,j=0;
262     for(i=0;i<vtx->tristar_no;i++){
263         if(vtx->tristar[i]!=tristar){
264             vtx->tristar[j]=vtx->tristar[i];
265             j++;
266         }
267     }
268     vtx->tristar_no--;
269     vtx->tristar=realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *));
270     if(vtx->neigh == NULL){
271             fatal("Reallocation of memory failed during insertion of vertex neighbour in vertex_add_neighbour",3);
272         }
273     return TS_SUCCESS;
274 }
275
276
277
f74313 278 /* ****************************************************************** */
SP 279 /* ***** New vertex copy operations. Inherently they are slow.  ***** */
280 /* ****************************************************************** */
281
b01cc1 282 ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){
f74313 283     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
8f6a69 284     cvtx->neigh=NULL;
SP 285     cvtx->neigh_no=0;
286     cvtx->tristar_no=0;
287     cvtx->bond_no=0;
288     cvtx->tristar=NULL;
289     cvtx->bond=NULL;
290     cvtx->cell=NULL;
b01cc1 291     return TS_SUCCESS;
f74313 292 }
dac2e5 293
SP 294 ts_bool vtx_duplicate(ts_vertex *cvtx, ts_vertex *ovtx){
295     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
296     return TS_SUCCESS;
297 }
298
f74313 299 //TODO: needs to be done
SP 300 ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){
301         return NULL;
302 }
303
dac2e5 304
SP 305
f74313 306 ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){
SP 307     ts_uint i;
308     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
b01cc1 309     vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list));
f74313 310     ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *));
b01cc1 311     vlist->vtx=vtx;
8f6a69 312     if(vlist->vtx==NULL)
b01cc1 313         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
SP 314     for(i=0;i<vlist->n;i++) {
8f6a69 315         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
b01cc1 316         vlist->vtx[i]->idx=i;
SP 317         vtx_copy(vlist->vtx[i],ovlist->vtx[i]);
f74313 318     }
b01cc1 319
f74313 320     return vlist;
SP 321 }