Trisurf Monte Carlo simulator
Samo Penic
2012-02-23 314f2dd60176b0f378baa66f5a5514fb15dddabc
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;
a10dd5 11     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
73f967 12     
d7639a 13     if(N==0){
SP 14         err("Initialized vertex list with zero elements. Pointer set to NULL");
73f967 15         vlist->n=0;
SP 16         vlist->vtx=NULL;
17         return vlist;
d7639a 18     }
73f967 19     
8f6a69 20     vlist->vtx=(ts_vertex **)calloc(N,sizeof(ts_vertex *));
SP 21     if(vlist->vtx==NULL)
73f967 22         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
a10dd5 23     for(i=0;i<N;i++) {
8f6a69 24         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
a10dd5 25         vlist->vtx[i]->idx=i;
SP 26     }
73f967 27     vlist->n=N;
d7639a 28     return vlist;
SP 29 }
30
737714 31 ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx){
d7639a 32     ts_uint i;
SP 33     /* no neighbour can be null! */
34     if(vtx==NULL || nvtx==NULL) return TS_FAIL;
35     
36     /*if it is already a neighbour don't add it to the list */
8f6a69 37     for(i=0; i<vtx->neigh_no;i++){
SP 38         if(vtx->neigh[i]==nvtx) return TS_FAIL;
d7639a 39     }
8f6a69 40     ts_uint nn=++vtx->neigh_no;
SP 41     vtx->neigh=(ts_vertex **)realloc(vtx->neigh, nn*sizeof(ts_vertex *));
42     vtx->neigh[nn-1]=nvtx;
3131dc 43 /* This was a bug in creating DIPYRAMID (the neighbours were not in right
SP 44  * order).
45  */
d7639a 46     /* pa se sosedu dodamo vertex */
SP 47     /*if it is already a neighbour don't add it to the list */
3131dc 48 /*
737714 49     for(i=0; i<nvtx->data->neigh_no;i++){
SP 50         if(nvtx->data->neigh[i]==vtx) return TS_FAIL;
d7639a 51     } 
a10dd5 52     nn=++nvtx->data->neigh_no;
737714 53     nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *));
a10dd5 54     nvtx->data->neigh[nn-1]=vtx;
3131dc 55 */
d7639a 56
SP 57     return TS_SUCCESS;
58 }
a10dd5 59
9802f1 60 /* TODO: optimize this. test this. */
SP 61 ts_bool vtx_remove_neighbour(ts_vertex *vtx, ts_vertex *nvtx){
62 /* find a neighbour */
63 /* remove it from the list while shifting remaining neighbours up */
64     ts_uint i,j=0;
8f6a69 65     for(i=0;i<vtx->neigh_no;i++){
SP 66         if(vtx->neigh[i]!=nvtx){
67             vtx->neigh[j]=vtx->neigh[i];
9802f1 68             j++;
SP 69         }
70     }
71 /* resize memory. potentionally time consuming */
8f6a69 72     vtx->neigh_no--;
SP 73     vtx->neigh=(ts_vertex **)realloc(vtx->neigh,vtx->neigh_no*sizeof(ts_vertex *));
74     if(vtx->neigh == NULL && vtx->neigh_no!=0)
9802f1 75         fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100);
SP 76
77 /* repeat for the neighbour */
78 /* find a neighbour */
79 /* remove it from the list while shifting remaining neighbours up */
8f6a69 80     for(i=0;i<nvtx->neigh_no;i++){
SP 81         if(nvtx->neigh[i]!=vtx){
82             nvtx->neigh[j]=nvtx->neigh[i];
9802f1 83             j++;
SP 84         }
85     }
86 /* resize memory. potentionally time consuming. */
8f6a69 87     nvtx->neigh_no--;
SP 88     nvtx->neigh=(ts_vertex **)realloc(nvtx->neigh,nvtx->neigh_no*sizeof(ts_vertex *));
89     if(nvtx->neigh == NULL && nvtx->neigh_no!=0)
9802f1 90         fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100);
SP 91
92     return TS_SUCCESS;
93 }
7958e9 94
9802f1 95
SP 96
a10dd5 97 ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){
SP 98     ts_bond *bond;
99     bond=bond_add(blist,vtx1,vtx2);
100     if(bond==NULL) return TS_FAIL;
8f6a69 101     vtx1->bond_no++;
3131dc 102    // vtx2->data->bond_no++;
a10dd5 103
8f6a69 104     vtx1->bond=(ts_bond **)realloc(vtx1->bond, vtx1->bond_no*sizeof(ts_bond *)); 
3131dc 105    // vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); 
8f6a69 106     vtx1->bond[vtx1->bond_no-1]=bond;
SP 107    // vtx2->ata->bond[vtx2->data->bond_no-1]=bond;
a10dd5 108     return TS_SUCCESS;
SP 109 }
110
111 ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
112     ts_bool retval;
113     retval=vtx_add_neighbour(vtx1,vtx2);
114     if(retval==TS_SUCCESS)
115     retval=vtx_add_bond(blist,vtx1,vtx2); 
116     return retval;
117 }
118
9802f1 119 /*TODO: write and optimize this urgently before use! */
SP 120 ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex
121 *vtx2){
122 //    ts_bool retval;
123 /* remove the bond */
124 //retval=vtx_remove_bond(blist,vtx1,vtx2);
125 /* remove the vertices */
126     return TS_SUCCESS;
127 }
a10dd5 128
d7639a 129
737714 130 ts_bool vtx_free(ts_vertex  *vtx){
8f6a69 131     if(vtx->neigh!=NULL)   free(vtx->neigh);
SP 132     if(vtx->tristar!=NULL) free(vtx->tristar);
133     if(vtx->bond!=NULL)    free(vtx->bond);
134
737714 135     free(vtx);
d7639a 136     return TS_SUCCESS;
SP 137 }
138
73f967 139 ts_bool vtx_list_free(ts_vertex_list *vlist){
SP 140     int i;
141     for(i=0;i<vlist->n;i++){
8f6a69 142         if(vlist->vtx[i]!=NULL) vtx_free(vlist->vtx[i]);
73f967 143     }
8f6a69 144     //free(*(vlist->vtx));
737714 145     free(vlist->vtx);
73f967 146     free(vlist);
SP 147     return TS_SUCCESS;
148 }
d7639a 149
bb77ca 150 inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){
d7639a 151     ts_double dist;
SP 152 #ifdef TS_DOUBLE_DOUBLE
8f6a69 153     dist=pow(vtx1->x-vtx2->x,2) + pow(vtx1->y-vtx2->y,2) + pow(vtx1->z-vtx2->z,2);
d7639a 154 #endif
SP 155 #ifdef TS_DOUBLE_LONGDOUBLE
8f6a69 156     dist=powl(vtx1->x-vtx2->x,2) + powl(vtx1->y-vtx2->y,2) + powl(vtx1->z-vtx2->z,2);
d7639a 157 #endif
SP 158 #ifdef TS_DOUBLE_FLOAT
8f6a69 159     dist=powf(vtx1->x-vtx2->x,2) + powf(vtx1->y-vtx2->y,2) + powf(vtx1->z-vtx2->z,2);
d7639a 160 #endif
SP 161     return(dist);
162 }
bb77ca 163
7958e9 164
SP 165
166 ts_bool vtx_set_global_values(ts_vesicle *vesicle){ 
167     ts_double xk=vesicle->bending_rigidity;
168     ts_uint i; 
169     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 170         vesicle->vlist->vtx[i]->xk=xk;
7958e9 171     }
SP 172     return TS_SUCCESS;
173 }
174
175 inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){
8f6a69 176     ts_double dX2=vtx2->x-vtx1->x;
SP 177     ts_double dY2=vtx2->y-vtx1->y;
178     ts_double dZ2=vtx2->z-vtx1->z;
179     ts_double dX3=vtx3->x-vtx1->x;
180     ts_double dY3=vtx3->y-vtx1->y;
181     ts_double dZ3=vtx3->z-vtx1->z;
182     ts_double direct=vtx1->x*(dY2*dZ3 -dZ2*dY3)+ 
183         vtx1->y*(dZ2*dX3-dX2*dZ3)+
184         vtx1->z*(dX2*dY3-dY2*dX3);
7958e9 185     return(direct);    
SP 186 }
187
188
189 inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem){
8f6a69 190     vtx->tristar_no++;
SP 191     vtx->tristar=(ts_triangle **)realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *));
192     if(vtx->tristar==NULL){
7958e9 193             fatal("Reallocation of memory while adding tristar failed.",3);
SP 194     }
8f6a69 195     vtx->tristar[vtx->tristar_no-1]=tristarmem;
7958e9 196     return TS_SUCCESS;
SP 197 }
198
f74313 199
SP 200 /* ****************************************************************** */
201 /* ***** New vertex copy operations. Inherently they are slow.  ***** */
202 /* ****************************************************************** */
203
b01cc1 204 ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){
f74313 205     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
8f6a69 206     cvtx->neigh=NULL;
SP 207     cvtx->neigh_no=0;
208     cvtx->tristar_no=0;
209     cvtx->bond_no=0;
210     cvtx->tristar=NULL;
211     cvtx->bond=NULL;
212     cvtx->cell=NULL;
b01cc1 213     return TS_SUCCESS;
f74313 214 }
dac2e5 215
SP 216 ts_bool vtx_duplicate(ts_vertex *cvtx, ts_vertex *ovtx){
217     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
218     return TS_SUCCESS;
219 }
220
f74313 221 //TODO: needs to be done
SP 222 ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){
223         return NULL;
224 }
225
dac2e5 226
SP 227
f74313 228 ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){
SP 229     ts_uint i;
230     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
b01cc1 231     vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list));
f74313 232     ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *));
b01cc1 233     vlist->vtx=vtx;
8f6a69 234     if(vlist->vtx==NULL)
b01cc1 235         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
SP 236     for(i=0;i<vlist->n;i++) {
8f6a69 237         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
b01cc1 238         vlist->vtx[i]->idx=i;
SP 239         vtx_copy(vlist->vtx[i],ovlist->vtx[i]);
f74313 240     }
b01cc1 241
f74313 242     return vlist;
SP 243 }