Trisurf Monte Carlo simulator
Samo Penic
2010-11-27 73f967e943c8d7a77cb21411de990904f09baba6
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"
6 #include<stdio.h>
7
73f967 8 ts_vertex_list *init_vertex_list(ts_uint N){    
d7639a 9     ts_int i;
73f967 10     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list *));
SP 11     
d7639a 12     if(N==0){
SP 13         err("Initialized vertex list with zero elements. Pointer set to NULL");
73f967 14         vlist->n=0;
SP 15         vlist->vtx=NULL;
16         return vlist;
d7639a 17     }
73f967 18     
SP 19     vlist->vtx=(ts_vertex **)malloc(N*sizeof(ts_vertex *));
20     if(vlist->vtx==NULL)
21         fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
22     for(i=0;i<N;i++) vlist->vtx[i]=init_vertex(i);
23     vlist->n=N;
d7639a 24     return vlist;
SP 25 }
26
27 ts_vertex *init_vertex(ts_uint idx){
28     ts_vertex *vtx;
29     vtx=(ts_vertex *)malloc(sizeof(ts_vertex));
30     if(vtx==NULL)
31         fatal("Fatal error reserving memory space for ts_vertex! Memory full?", 100);
32     vtx->idx=idx;
33     return vtx;
34 }
35
36 /*
37 ts_bool vtx_set_global_values(ts_vertex **vlist, ts_vesicle *vesicle){
38     ts_double xk=vesicle->bending_rigidity;
39     ts_uint i;
40     for(i=0;i<vesicle->vlist.n;i++){
41         vlist[i]->xk=xk;
42     }
43     return TS_SUCCESS;
44 }
45 */
46
47
48
49 ts_bool vtx_add_neighbour(ts_vertex **vtx, ts_vertex **nvtx){
50     ts_uint i;
51     /* no neighbour can be null! */
52     if(vtx==NULL || nvtx==NULL) return TS_FAIL;
53     
54     /*if it is already a neighbour don't add it to the list */
55     for(i=0; i<(*vtx)->neigh_no;i++){
56         if((*vtx)->neigh[i]==nvtx) return TS_FAIL;
57     }
58     ts_uint nn=(*vtx)->neigh_no++;
59     (*vtx)->neigh=(ts_vertex ***)realloc((*vtx)->neigh, nn*sizeof(ts_vertex **));
60     (*vtx)->neigh[nn]=nvtx;
61
62     /* pa se sosedu dodamo vertex */
63     /*if it is already a neighbour don't add it to the list */
64     for(i=0; i<(*nvtx)->neigh_no;i++){
65         if((*nvtx)->neigh[i]==vtx) return TS_FAIL;
66     } 
67     nn=(*nvtx)->neigh_no++;
68     (*nvtx)->neigh=(ts_vertex ***)realloc((*nvtx)->neigh, nn*sizeof(ts_vertex **));
69     (*nvtx)->neigh[nn]=vtx;
70
71
72 /* Ustvari bond in doloci dolzino */
73
74     return TS_SUCCESS;
75 }
76
77
78 ts_bool vtx_free(ts_vertex **vtx){
79     if((*vtx)->neigh!=NULL)   free((*vtx)->neigh);
80     if((*vtx)->tristar!=NULL) free((*vtx)->tristar);
81     if((*vtx)->bond!=NULL)    free((*vtx)->bond);
82     if((*vtx)->cell!=NULL)    free((*vtx)->cell);
83     free(*vtx);
84     return TS_SUCCESS;
85 }
86
73f967 87 ts_bool vtx_list_free(ts_vertex_list *vlist){
SP 88     int i;
89     for(i=0;i<vlist->n;i++){
90         vtx_free(VTX(vlist,i));
91     }
92     free(vlist);
93     return TS_SUCCESS;
94 }
d7639a 95
73f967 96
SP 97
98
99 inline ts_double vtx_distance_sq(ts_vertex **vtx1, ts_vertex **vtx2){
d7639a 100     ts_double dist;
SP 101 #ifdef TS_DOUBLE_DOUBLE
102     dist=pow((*vtx1)->x-(*vtx2)->x,2) + pow((*vtx1)->y-(*vtx2)->y,2) + pow((*vtx1)->z-(*vtx2)->z,2);
103 #endif
104 #ifdef TS_DOUBLE_LONGDOUBLE
105     dist=powl((*vtx1)->x-(*vtx2)->x,2) + powl((*vtx1)->y-(*vtx2)->y,2) + powl((*vtx1)->z-(*vtx2)->z,2);
106 #endif
107 #ifdef TS_DOUBLE_FLOAT
108     dist=powf((*vtx1)->x-(*vtx2)->x,2) + powf((*vtx1)->y-(*vtx2)->y,2) + powf((*vtx1)->z-(*vtx2)->z,2);
109 #endif
110     return(dist);
111 }
112