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