Trisurf Monte Carlo simulator
Samo
2012-07-10 a0f0a3ac3363a9cc058e1de3f64dac2598d9c787
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;
074a17 26
SP 27     /* initialize Ylm for spherical hamonics DONE in sh.c */
28 /*    for(i=0;i<l;i++){
29         vlist->vtx[i]->Ylm[i]=(ts_double **)calloc(2*i+1,sizeof(ts_double *));
30         for(j=0;j<(2*i+1);j++){
31             clist->vtx[i]->Ylm[i][j]=(ts_double *)calloc(sizeof(ts_double));
32         }
33     }
34 */
35
36
a10dd5 37     }
73f967 38     vlist->n=N;
d7639a 39     return vlist;
SP 40 }
41
737714 42 ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx){
d7639a 43     ts_uint i;
SP 44     /* no neighbour can be null! */
45     if(vtx==NULL || nvtx==NULL) return TS_FAIL;
46     
47     /*if it is already a neighbour don't add it to the list */
8f6a69 48     for(i=0; i<vtx->neigh_no;i++){
SP 49         if(vtx->neigh[i]==nvtx) return TS_FAIL;
d7639a 50     }
8f6a69 51     ts_uint nn=++vtx->neigh_no;
SP 52     vtx->neigh=(ts_vertex **)realloc(vtx->neigh, nn*sizeof(ts_vertex *));
53     vtx->neigh[nn-1]=nvtx;
3131dc 54 /* This was a bug in creating DIPYRAMID (the neighbours were not in right
SP 55  * order).
56  */
d7639a 57     /* pa se sosedu dodamo vertex */
SP 58     /*if it is already a neighbour don't add it to the list */
3131dc 59 /*
737714 60     for(i=0; i<nvtx->data->neigh_no;i++){
SP 61         if(nvtx->data->neigh[i]==vtx) return TS_FAIL;
d7639a 62     } 
a10dd5 63     nn=++nvtx->data->neigh_no;
737714 64     nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *));
a10dd5 65     nvtx->data->neigh[nn-1]=vtx;
3131dc 66 */
d7639a 67
SP 68     return TS_SUCCESS;
69 }
a10dd5 70
9802f1 71 /* TODO: optimize this. test this. */
SP 72 ts_bool vtx_remove_neighbour(ts_vertex *vtx, ts_vertex *nvtx){
73 /* find a neighbour */
74 /* remove it from the list while shifting remaining neighbours up */
75     ts_uint i,j=0;
8f6a69 76     for(i=0;i<vtx->neigh_no;i++){
SP 77         if(vtx->neigh[i]!=nvtx){
78             vtx->neigh[j]=vtx->neigh[i];
9802f1 79             j++;
SP 80         }
81     }
82 /* resize memory. potentionally time consuming */
8f6a69 83     vtx->neigh_no--;
SP 84     vtx->neigh=(ts_vertex **)realloc(vtx->neigh,vtx->neigh_no*sizeof(ts_vertex *));
85     if(vtx->neigh == NULL && vtx->neigh_no!=0)
9802f1 86         fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100);
SP 87
88 /* repeat for the neighbour */
89 /* find a neighbour */
90 /* remove it from the list while shifting remaining neighbours up */
8f6a69 91     for(i=0;i<nvtx->neigh_no;i++){
SP 92         if(nvtx->neigh[i]!=vtx){
93             nvtx->neigh[j]=nvtx->neigh[i];
9802f1 94             j++;
SP 95         }
96     }
97 /* resize memory. potentionally time consuming. */
8f6a69 98     nvtx->neigh_no--;
SP 99     nvtx->neigh=(ts_vertex **)realloc(nvtx->neigh,nvtx->neigh_no*sizeof(ts_vertex *));
100     if(nvtx->neigh == NULL && nvtx->neigh_no!=0)
9802f1 101         fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100);
SP 102
103     return TS_SUCCESS;
104 }
7958e9 105
9802f1 106
SP 107
a10dd5 108 ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){
SP 109     ts_bond *bond;
110     bond=bond_add(blist,vtx1,vtx2);
111     if(bond==NULL) return TS_FAIL;
8f6a69 112     vtx1->bond_no++;
3131dc 113    // vtx2->data->bond_no++;
a10dd5 114
8f6a69 115     vtx1->bond=(ts_bond **)realloc(vtx1->bond, vtx1->bond_no*sizeof(ts_bond *)); 
3131dc 116    // vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); 
8f6a69 117     vtx1->bond[vtx1->bond_no-1]=bond;
SP 118    // vtx2->ata->bond[vtx2->data->bond_no-1]=bond;
a10dd5 119     return TS_SUCCESS;
SP 120 }
121
122 ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
123     ts_bool retval;
124     retval=vtx_add_neighbour(vtx1,vtx2);
125     if(retval==TS_SUCCESS)
126     retval=vtx_add_bond(blist,vtx1,vtx2); 
127     return retval;
128 }
129
9802f1 130 /*TODO: write and optimize this urgently before use! */
SP 131 ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex
132 *vtx2){
133 //    ts_bool retval;
134 /* remove the bond */
135 //retval=vtx_remove_bond(blist,vtx1,vtx2);
136 /* remove the vertices */
137     return TS_SUCCESS;
138 }
a10dd5 139
d7639a 140
737714 141 ts_bool vtx_free(ts_vertex  *vtx){
8f6a69 142     if(vtx->neigh!=NULL)   free(vtx->neigh);
SP 143     if(vtx->tristar!=NULL) free(vtx->tristar);
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
SP 210 /* ****************************************************************** */
211 /* ***** New vertex copy operations. Inherently they are slow.  ***** */
212 /* ****************************************************************** */
213
b01cc1 214 ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){
f74313 215     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
8f6a69 216     cvtx->neigh=NULL;
SP 217     cvtx->neigh_no=0;
218     cvtx->tristar_no=0;
219     cvtx->bond_no=0;
220     cvtx->tristar=NULL;
221     cvtx->bond=NULL;
222     cvtx->cell=NULL;
b01cc1 223     return TS_SUCCESS;
f74313 224 }
dac2e5 225
SP 226 ts_bool vtx_duplicate(ts_vertex *cvtx, ts_vertex *ovtx){
227     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
228     return TS_SUCCESS;
229 }
230
f74313 231 //TODO: needs to be done
SP 232 ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){
233         return NULL;
234 }
235
dac2e5 236
SP 237
f74313 238 ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){
SP 239     ts_uint i;
240     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
b01cc1 241     vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list));
f74313 242     ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *));
b01cc1 243     vlist->vtx=vtx;
8f6a69 244     if(vlist->vtx==NULL)
b01cc1 245         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
SP 246     for(i=0;i<vlist->n;i++) {
8f6a69 247         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
b01cc1 248         vlist->vtx[i]->idx=i;
SP 249         vtx_copy(vlist->vtx[i],ovlist->vtx[i]);
f74313 250     }
b01cc1 251
f74313 252     return vlist;
SP 253 }