From b01cc1ac677dc712c27337be8ee4f11ea336f31b Mon Sep 17 00:00:00 2001
From: Samo Penic <samo@andromeda>
Date: Sun, 05 Dec 2010 13:02:22 +0000
Subject: [PATCH] Fixed some nasty bugs. Still, I suspect the vertex neighbours are not correctly oriented or something, since tristar triangles are not successfully set. (tristar_no is not always the same as neigh_no!)

---
 src/cell.c                 |    2 
 src/main.c                 |    6 ++-
 src/vertex.c               |   22 +++++++----
 src/initial_distribution.h |    2 
 src/bond.c                 |    9 +++-
 src/energy.c               |    2 +
 src/initial_distribution.c |   46 +++++++++++------------
 src/vertex.h               |    2 
 8 files changed, 52 insertions(+), 39 deletions(-)

diff --git a/src/bond.c b/src/bond.c
index 289079a..1478720 100644
--- a/src/bond.c
+++ b/src/bond.c
@@ -10,10 +10,15 @@
 }
 
 ts_bond  *bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
-    
+    ts_uint i;
     /* no vertices must be null! */
     if(vtx1==NULL || vtx2==NULL) return NULL;
-    /* TODO: Verify if the bond already exists... Don't do multiple bonds */
+    /* Verify if the bond already exists... Don't do multiple bonds */
+    for(i=0;i<blist->n;i++){
+       if((blist->bond[i]->data->vtx1==vtx1 || blist->bond[i]->data->vtx1==vtx2)
+        &&(blist->bond[i]->data->vtx2==vtx1 || blist->bond[i]->data->vtx2==vtx2))
+        return blist->bond[i];
+    }
 	blist->n++;
 	blist->bond=(ts_bond **)realloc(blist->bond,blist->n*sizeof(ts_bond *));
 	if(blist->bond==NULL) fatal("Cannot reallocate memory for additional **ts_bond.",100);
diff --git a/src/cell.c b/src/cell.c
index b3d90e4..26ce2d6 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -77,7 +77,7 @@
     cell->data->nvertex++;
 	cell->data->vertex=(ts_vertex **)realloc(cell->data->vertex,cell->data->nvertex*sizeof(ts_vertex *));
 		if(vtx->data->neigh == NULL){
-			fatal("Reallocation of memory failed during insertion of vertex neighbour in vertex_add_neighbour",3);
+			fatal("Reallocation of memory failed during insertion of vertex in cell_add_vertex",3);
         }
     cell->data->vertex[cell->data->nvertex-1]=vtx;
     return TS_SUCCESS;
diff --git a/src/energy.c b/src/energy.c
index 540686b..e30295b 100644
--- a/src/energy.c
+++ b/src/energy.c
@@ -13,6 +13,7 @@
 
     for(i=0;i<vlist->n;i++){
         energy_vertex(vtx[i]);
+        
     }
 
     return TS_SUCCESS;
@@ -38,6 +39,7 @@
         j=data->neigh[jj-1];
         jp=data->neigh[jjp-1];
         jm=data->neigh[jjm-1];
+//        printf("tristar_no=%u, neigh_no=%u, jj=%u\n",data->tristar_no,data->neigh_no,jj);
         jt=data->tristar[jj-1];
         x1=vtx_distance_sq(vtx,jp); //shouldn't be zero!
         x2=vtx_distance_sq(j,jp); // shouldn't be zero!
diff --git a/src/initial_distribution.c b/src/initial_distribution.c
index 4881e11..6a13a23 100644
--- a/src/initial_distribution.c
+++ b/src/initial_distribution.c
@@ -20,8 +20,8 @@
     retval = vtx_set_global_values(vesicle);
     retval = pentagonal_dipyramid_vertex_distribution(vesicle->vlist);
     retval = init_vertex_neighbours(vesicle->vlist);
-    retval = init_sort_neighbours(vesicle->vlist);
-    retval = init_vesicle_bonds(vesicle);
+    vesicle->vlist = init_sort_neighbours(vesicle->blist,vesicle->vlist);
+   // retval = init_vesicle_bonds(vesicle); // bonds are created in sort_neigh
     retval = init_triangles(vesicle);
     retval = init_triangle_neighbours(vesicle);
     retval = init_common_vertex_triangle_neighbours(vesicle);
@@ -150,21 +150,21 @@
 	return TS_SUCCESS;
 }
 
-// TODO: with new datastructure can be rewritten.
-ts_bool init_sort_neighbours(ts_vertex_list *vlist){
+// TODO: with new datastructure can be rewritten. Partially it is done, but it is complicated.
+ts_vertex_list *init_sort_neighbours(ts_bond_list *blist,ts_vertex_list *vlist){
 	ts_vertex **vtx=vlist->vtx -1; // take a look at dipyramid function for comment.
 	ts_uint i,l,j,jj,jjj,k=0;   
     ts_double eps=0.001; // Take a look if EPS from math.h can be used
 
 /*lets initialize memory for temporary vertex_list. Should we write a function instead */
-    ts_vertex_list *tvlist=init_vertex_list(vlist->n);
+    ts_vertex_list *tvlist=vertex_list_copy(vlist);
     ts_vertex **tvtx=tvlist->vtx -1;  /* again to compensate for 0-indexing */
 
 	ts_double dist2; // Square of distance of neighbours
     ts_double direct; // Something, dont know what, but could be normal of some kind
 	for(i=1;i<=vlist->n;i++){
 		k++; // WHY i IS NOT GOOD??
-       	vtx_add_neighbour(tvtx[k], tvtx[vtx[i]->data->neigh[0]->idx+1]); //always add 1st
+       	vtx_add_cneighbour(blist,tvtx[k], tvtx[vtx[i]->data->neigh[0]->idx+1]); //always add 1st
        	jjj=1;
        	jj=1;
        	for(l=2;l<=vtx[i]->data->neigh_no;l++){
@@ -172,7 +172,7 @@
                	dist2=vtx_distance_sq(vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);
                	direct=vtx_direct(vtx[i],vtx[i]->data->neigh[j-1],vtx[i]->data->neigh[jj-1]);
                	if( (fabs(dist2-A0*A0)<=eps) && (direct>0.0) && (j!=jjj) ){
-           			vtx_add_neighbour(tvtx[k],tvtx[vtx[i]->data->neigh[j-1]->idx+1]);
+           			vtx_add_cneighbour(blist,tvtx[k],tvtx[vtx[i]->data->neigh[j-1]->idx+1]);
            			jjj=jj;
            			jj=j;
            			break;
@@ -180,24 +180,19 @@
        		}
        	}	
 	}
-
-    for(i=1;i<=vlist->n;i++){
-        for(j=1;j<=vtx[i]->data->neigh_no;j++){
-            if(vtx[i]->data->neigh_no!=tvtx[i]->data->neigh_no){ //doesn't work with nshell=1!
-//                fprintf(stderr,"data1=%u data2=%u\n",vtx[i]->data->neigh_no,tvtx[i]->data->neigh_no);
-                fatal("Number of neighbours not the same in init_sort_neighbours.",4);
-            }
-            //we must correct the pointers in original to point to their
-            //neighbours according to indexes. Must be sure not to do it any
-            //other way! Also, we need to repair the collection of bonds...
-            vtx[i]->data->neigh[j-1]=vtx[tvtx[i]->data->neigh[j-1]->idx+1];
-        }
+/* We use the temporary vertex for our main vertices and we abandon main
+ * vertices, because their neighbours are not correctly ordered */
+   // tvtx=vlist->vtx;
+   // vlist->vtx=tvtx;
+   // tvlist->vtx=vtx;
+    vtx_list_free(vlist);
+/* Let's make a check if the number of bonds is correct */
+    if((blist->n)!=3*(tvlist->n-2)){
+        ts_fprintf(stderr,"Number of bonds is %u should be %u!\n", blist->n, 3*(tvlist->n-2));
+        fatal("Number of bonds is not 3*(no_vertex-2).",4);
     }
 
-   // Must free memory for temporary vertex array to avoid memory leak! HERE! NOW!
-//    free_vertex(tvlist.vertex,tvlist.n);
-    vtx_list_free(tvlist);
-	return TS_SUCCESS;
+	return tvlist;
 }
 
 
@@ -350,7 +345,10 @@
                 if((vtx[i]==k3 && k1==k4 && k2==k5) ||
                 (vtx[i]==k4 && k1==k5 && k2==k3) ||
                 (vtx[i]==k5 && k1==k3 && k2==k4)){
-          //          ts_fprintf(stderr, "Added to tristar! ");
+
+//TODO: probably something wrong with neighbour distribution.
+//                if(vtx[i]==k3 || vtx[i]==k4 || vtx[i]==k5){
+                    if(i==6) ts_fprintf(stdout, "Vtx[%u] > Added to tristar!\n",i);
                     vertex_add_tristar(vtx[i],tria[k]);
                 }
             }
diff --git a/src/initial_distribution.h b/src/initial_distribution.h
index 04fdfe1..7b3e2ba 100644
--- a/src/initial_distribution.h
+++ b/src/initial_distribution.h
@@ -27,7 +27,7 @@
 //ts_bool init_vertex_neighbours(ts_vertex_list *vlist);
 
 /** interior sites and their neighbours in circ. order + the triangles they are holding together */
-ts_bool init_sort_neighbours(ts_vertex_list *vlist);
+ts_vertex_list *init_sort_neighbours(ts_bond_list *blist,ts_vertex_list *vlist);
 ts_bool init_vesicle_bonds(ts_vesicle *vesicle);
 ts_bool init_triangles(ts_vesicle *vesicle);
 ts_bool init_triangle_neighbours(ts_vesicle *vesicle);
diff --git a/src/main.c b/src/main.c
index a66e81b..d0ee34d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,7 @@
 int main(int argv, char *argc[]){
 ts_bool retval;
 ts_vertex_list *vlist=init_vertex_list(5);
+ts_vertex_list *vlist1;
 ts_bond_list *blist=init_bond_list();
 ts_triangle_list *tlist=init_triangle_list();
 ts_cell_list *clist=init_cell_list(3,3,3,0.3);
@@ -34,7 +35,7 @@
 
 vlist->vtx[0]->data->x=1.0;
 vlist->vtx[0]->data->x=1.1;
-
+vlist1=vertex_list_copy(vlist);
 bond_add(blist, vlist->vtx[1],vlist->vtx[0]);
 triangle_add(tlist,vlist->vtx[1],vlist->vtx[2],vlist->vtx[3]);
 
@@ -53,8 +54,9 @@
 bond_list_free(blist);
 vtx_list_free(vlist);
 cell_list_free(clist);
-printf("Tests complete.\n");
 
+vtx_list_free(vlist1);
+printf("Tests complete.\n");
 vesicle=initial_distribution_dipyramid(7,10,10,10,0.3);
 write_vertex_xml_file(vesicle,0);
 write_master_xml_file("test.pvd");
diff --git a/src/vertex.c b/src/vertex.c
index dfb0155..92c5032 100644
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -156,7 +156,7 @@
 ts_bool vtx_list_free(ts_vertex_list *vlist){
     int i;
     for(i=0;i<vlist->n;i++){
-        vtx_data_free(vlist->vtx[i]->data);
+        if(vlist->vtx[i]->data!=NULL) vtx_data_free(vlist->vtx[i]->data);
     }
     free(*(vlist->vtx));
     free(vlist->vtx);
@@ -219,8 +219,7 @@
 /* ***** New vertex copy operations. Inherently they are slow.  ***** */
 /* ****************************************************************** */
 
-ts_vertex *vtx_copy(ts_vertex *ovtx){
-    ts_vertex *cvtx=(ts_vertex *)malloc(sizeof(ts_vertex));
+ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){
     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
     cvtx->data=(ts_vertex_data *)malloc(sizeof(ts_vertex_data));
     memcpy((void *)cvtx->data,(void *)ovtx->data,sizeof(ts_vertex_data));
@@ -230,8 +229,8 @@
     cvtx->data->bond_no=0;
     cvtx->data->tristar=NULL;
     cvtx->data->bond=NULL;
-    cvtx->data->cell=NULL; 
-    return cvtx;
+    cvtx->data->cell=NULL;
+    return TS_SUCCESS;
 }
 //TODO: needs to be done
 ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){
@@ -241,10 +240,17 @@
 ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){
     ts_uint i;
     ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
-    memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list));
+    vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list));
     ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *));
-    for(i=0;i<vlist->n;i++){
-        vtx[i]=vtx_copy(ovlist->vtx[i]);
+    ts_vertex *tlist=(ts_vertex *)calloc(vlist->n,sizeof(ts_vertex));
+    vlist->vtx=vtx;
+    if(vlist->vtx==NULL || tlist==NULL)
+        fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100);
+    for(i=0;i<vlist->n;i++) {
+        vlist->vtx[i]=&tlist[i];
+        vlist->vtx[i]->idx=i;
+        vtx_copy(vlist->vtx[i],ovlist->vtx[i]);
     }
+
     return vlist;
 }
diff --git a/src/vertex.h b/src/vertex.h
index 0b353aa..0c61c85 100644
--- a/src/vertex.h
+++ b/src/vertex.h
@@ -27,7 +27,7 @@
 inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3);
 inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem);
 
-ts_vertex *vtx_copy(ts_vertex *ovtx);
+ts_bool vtx_copy(ts_vertex *cvtx,ts_vertex *ovtx);
 ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx);
 ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist);
 

--
Gitblit v1.9.3