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