Trisurf Monte Carlo simulator
Samo Penic
2013-11-30 bc4e1ee61725fdf7ec81a9583b7fa545f847e07b
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
73f967 9 ts_vertex_list *init_vertex_list(ts_uint N){    
d7639a 10     ts_int i;
bba37c 11     ts_vertex_list *vlist=(ts_vertex_list *)calloc(1,sizeof(ts_vertex_list));
SP 12  
d7639a 13     if(N==0){
bba37c 14             vlist->n=0;
SP 15         vlist->vtx=(ts_vertex **)calloc(1,sizeof(ts_vertex *)); /*Allocate one memory space for vlist anyway */
73f967 16         return vlist;
d7639a 17     }
73f967 18     
8f6a69 19     vlist->vtx=(ts_vertex **)calloc(N,sizeof(ts_vertex *));
SP 20     if(vlist->vtx==NULL)
73f967 21         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
a10dd5 22     for(i=0;i<N;i++) {
8f6a69 23         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
a10dd5 24         vlist->vtx[i]->idx=i;
8ed8fd 25     vlist->vtx[i]->neigh=init_vertex_list(0);
bba37c 26         /* initialize Ylm for spherical hamonics DONE in sh.c */
SP 27         }
73f967 28     vlist->n=N;
8ed8fd 29     vlist->list_size=TS_VLIST_CHUNK; //TODO: can be buggy in some cases, when N>0 and we want to delete some vertices.
d7639a 30     return vlist;
SP 31 }
32
64c113 33
SP 34 ts_bool vertex_list_add_vtx(ts_vertex_list *vlist, ts_vertex *vtx){
35
36 #ifdef DEBUG
8ed8fd 37     if(vtx==NULL)
SP 38     err("VTX is null");
39     if(vlist==NULL) err("VLIST is null");
40
41     if(vtx==NULL || vlist==NULL)
42         return TS_FAIL;
64c113 43 #endif
SP 44     if(vlist->list_size < vlist->n+1){
45         vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size+TS_VLIST_CHUNK)*sizeof(ts_vertex*));
46         if(vlist->vtx==NULL){
47             fatal("Error in vertex_list_add. Could not reallocate memory space.",9999);
48         }
49         vlist->list_size+=TS_VLIST_CHUNK;
50     }
8ed8fd 51 //    printf("Test %u (max %u)!\n", vlist->n, vlist->list_size);
bba37c 52     vlist->vtx[vlist->n]=vtx;
64c113 53     vlist->n++;
SP 54     return TS_SUCCESS;
55 }
56
57
58 /* Idea is to delete vertex by removing it from list. The emply place is then filled in by the last 
59 vertex in the list. List can then be resized by one -- unless resize is required when max list size - 
60 real list size > predefined value */
61 ts_bool vertex_list_remove_vtx(ts_vertex_list *vlist, ts_vertex *vtx){
62 #ifdef DEBUG
63     if(vtx==NULL || vlist==NULL) return TS_FAIL;
64 #endif
bba37c 65     ts_uint i;
64c113 66     for(i=0; i<vlist->n;i++){
SP 67         if(vlist->vtx[i]==vtx){
bba37c 68             vlist->vtx[i]=vlist->vtx[vlist->n-1];
64c113 69             vlist->n--;
SP 70             if(vlist->list_size-vlist->n > TS_VLIST_CHUNK){
71                 vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size-TS_VLIST_CHUNK)*sizeof(ts_vertex*));
72                 if(vlist->vtx==NULL){
73                     fatal("Error in vertex_list_add. Could not reallocate memory space.",9999);
74                 }
75                 vlist->list_size-=TS_VLIST_CHUNK;
76             }
77             return TS_SUCCESS;
78         }
79     }
80     return TS_FAIL;
9802f1 81 }
7958e9 82
9802f1 83
SP 84
a10dd5 85 ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){
SP 86     ts_bond *bond;
87     bond=bond_add(blist,vtx1,vtx2);
88     if(bond==NULL) return TS_FAIL;
8f6a69 89     vtx1->bond_no++;
30ee9c 90     vtx2->bond_no++;
3131dc 91    // vtx2->data->bond_no++;
a10dd5 92
8f6a69 93     vtx1->bond=(ts_bond **)realloc(vtx1->bond, vtx1->bond_no*sizeof(ts_bond *)); 
30ee9c 94     vtx2->bond=(ts_bond **)realloc(vtx2->bond, vtx2->bond_no*sizeof(ts_bond *)); 
3131dc 95    // vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); 
8f6a69 96     vtx1->bond[vtx1->bond_no-1]=bond;
30ee9c 97     vtx2->bond[vtx2->bond_no-1]=bond;
8f6a69 98    // vtx2->ata->bond[vtx2->data->bond_no-1]=bond;
a10dd5 99     return TS_SUCCESS;
SP 100 }
101
bc4e1e 102 // Add neighbour just in one direction (add vtx2 as a neigh of vtx1 and not another way around!
2f8ee7 103 ts_bool vtx_add_neighbour(ts_vertex *vtx1, ts_vertex *vtx2){
8ed8fd 104     ts_uint i;
SP 105     if(vtx1==NULL || vtx2==NULL) return TS_FAIL;
106     for(i=0;i<vtx1->neigh->n;i++){
107         if (vtx1->neigh->vtx[i]==vtx2){
108             return TS_FAIL;
109         }
110     }
111         vertex_list_add_vtx(vtx1->neigh, vtx2);
2f8ee7 112     return TS_SUCCESS;
SP 113 }
114
115 ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
a10dd5 116     ts_bool retval;
8ed8fd 117     retval=vtx_add_neighbour(vtx1, vtx2);
SP 118 //    retval=vertex_list_add_vtx(vtx1->neigh, vtx2);
119 //    retval=vertex_list_add_vtx(vtx2->neigh, vtx1);
120     if(retval==TS_SUCCESS){
121     //    fprintf(stderr,"Bond added\n");
122        retval=vtx_add_bond(blist,vtx1,vtx2); 
123         }
a10dd5 124     return retval;
SP 125 }
126
9802f1 127 /*TODO: write and optimize this urgently before use! */
SP 128 ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex
129 *vtx2){
130 //    ts_bool retval;
131 /* remove the bond */
132 //retval=vtx_remove_bond(blist,vtx1,vtx2);
133 /* remove the vertices */
134     return TS_SUCCESS;
135 }
a10dd5 136
d7639a 137
737714 138 ts_bool vtx_free(ts_vertex  *vtx){
8ed8fd 139     if(vtx->neigh!=NULL)  {
SP 140         if (vtx->neigh->vtx!=NULL) free(vtx->neigh->vtx);
141         free(vtx->neigh);
142     }
8f6a69 143     if(vtx->tristar!=NULL) free(vtx->tristar);
SP 144     if(vtx->bond!=NULL)    free(vtx->bond);
737714 145     free(vtx);
d7639a 146     return TS_SUCCESS;
SP 147 }
148
73f967 149 ts_bool vtx_list_free(ts_vertex_list *vlist){
SP 150     int i;
151     for(i=0;i<vlist->n;i++){
8f6a69 152         if(vlist->vtx[i]!=NULL) vtx_free(vlist->vtx[i]);
73f967 153     }
8f6a69 154     //free(*(vlist->vtx));
737714 155     free(vlist->vtx);
73f967 156     free(vlist);
SP 157     return TS_SUCCESS;
158 }
d7639a 159
bb77ca 160 inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){
d7639a 161     ts_double dist;
SP 162 #ifdef TS_DOUBLE_DOUBLE
8f6a69 163     dist=pow(vtx1->x-vtx2->x,2) + pow(vtx1->y-vtx2->y,2) + pow(vtx1->z-vtx2->z,2);
d7639a 164 #endif
SP 165 #ifdef TS_DOUBLE_LONGDOUBLE
8f6a69 166     dist=powl(vtx1->x-vtx2->x,2) + powl(vtx1->y-vtx2->y,2) + powl(vtx1->z-vtx2->z,2);
d7639a 167 #endif
SP 168 #ifdef TS_DOUBLE_FLOAT
8f6a69 169     dist=powf(vtx1->x-vtx2->x,2) + powf(vtx1->y-vtx2->y,2) + powf(vtx1->z-vtx2->z,2);
d7639a 170 #endif
SP 171     return(dist);
172 }
bb77ca 173
7958e9 174
SP 175
176 ts_bool vtx_set_global_values(ts_vesicle *vesicle){ 
177     ts_double xk=vesicle->bending_rigidity;
178     ts_uint i; 
179     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 180         vesicle->vlist->vtx[i]->xk=xk;
7958e9 181     }
SP 182     return TS_SUCCESS;
183 }
184
185 inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){
8f6a69 186     ts_double dX2=vtx2->x-vtx1->x;
SP 187     ts_double dY2=vtx2->y-vtx1->y;
188     ts_double dZ2=vtx2->z-vtx1->z;
189     ts_double dX3=vtx3->x-vtx1->x;
190     ts_double dY3=vtx3->y-vtx1->y;
191     ts_double dZ3=vtx3->z-vtx1->z;
192     ts_double direct=vtx1->x*(dY2*dZ3 -dZ2*dY3)+ 
193         vtx1->y*(dZ2*dX3-dX2*dZ3)+
194         vtx1->z*(dX2*dY3-dY2*dX3);
7958e9 195     return(direct);    
SP 196 }
197
198
199 inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem){
8f6a69 200     vtx->tristar_no++;
SP 201     vtx->tristar=(ts_triangle **)realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *));
202     if(vtx->tristar==NULL){
7958e9 203             fatal("Reallocation of memory while adding tristar failed.",3);
SP 204     }
8f6a69 205     vtx->tristar[vtx->tristar_no-1]=tristarmem;
7958e9 206     return TS_SUCCESS;
SP 207 }
208
f74313 209
30ee9c 210
SP 211 /* vtx remove tristar is required in  bondflip. */
212 /* TODO: Check whether it is important to keep the numbering of tristar
213  * elements in some order or not! */
214 inline ts_bool vtx_remove_tristar(ts_vertex *vtx, ts_triangle *tristar){
215     ts_uint i,j=0;
216     for(i=0;i<vtx->tristar_no;i++){
217         if(vtx->tristar[i]!=tristar){
218             vtx->tristar[j]=vtx->tristar[i];
219             j++;
220         }
221     }
222     vtx->tristar_no--;
223     vtx->tristar=realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *));
224     if(vtx->neigh == NULL){
225             fatal("Reallocation of memory failed during insertion of vertex neighbour in vertex_add_neighbour",3);
226         }
227     return TS_SUCCESS;
228 }
229
230
231
f74313 232 /* ****************************************************************** */
SP 233 /* ***** New vertex copy operations. Inherently they are slow.  ***** */
234 /* ****************************************************************** */
235
b01cc1 236 ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){
f74313 237     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
8ed8fd 238     cvtx->neigh=init_vertex_list(0);
8f6a69 239     cvtx->tristar_no=0;
SP 240     cvtx->bond_no=0;
241     cvtx->tristar=NULL;
242     cvtx->bond=NULL;
243     cvtx->cell=NULL;
b01cc1 244     return TS_SUCCESS;
f74313 245 }
dac2e5 246
SP 247 ts_bool vtx_duplicate(ts_vertex *cvtx, ts_vertex *ovtx){
248     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
249     return TS_SUCCESS;
250 }
251
f74313 252 //TODO: needs to be done
SP 253 ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){
254         return NULL;
255 }
256
dac2e5 257
SP 258
f74313 259 ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){
SP 260     ts_uint i;
261     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
b01cc1 262     vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list));
f74313 263     ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *));
b01cc1 264     vlist->vtx=vtx;
8f6a69 265     if(vlist->vtx==NULL)
b01cc1 266         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
SP 267     for(i=0;i<vlist->n;i++) {
8f6a69 268         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
b01cc1 269         vlist->vtx[i]->idx=i;
SP 270         vtx_copy(vlist->vtx[i],ovlist->vtx[i]);
f74313 271     }
b01cc1 272
f74313 273     return vlist;
SP 274 }