From 8ed8fdb663089e2bc53fd537611a6b45a09cc502 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sat, 30 Nov 2013 16:54:34 +0000
Subject: [PATCH] Seems to solve everything until bondflip

---
 src/Makefile.am |    4 ++++
 src/vertex.c    |   39 +++++++++++++++++++++++++++++----------
 src/general.h   |    2 +-
 3 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 1221acb..10ae3c2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,3 +15,7 @@
 spherical_trisurf_ffdir=../
 spherical_trisurf_ff_PROGRAMS = spherical_trisurf_ff
 spherical_trisurf_ff_SOURCES = general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c frame.c energy.c timestep.c vertexmove.c spherical_trisurf_ff.c sh.c bondflip.c
+neigh_testdir=../
+neigh_test_PROGRAMS = neigh_test
+neigh_test_SOURCES = general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c frame.c energy.c timestep.c vertexmove.c bondflip.c neigh_test.c
+
diff --git a/src/general.h b/src/general.h
index b1a7474..03dcc1d 100644
--- a/src/general.h
+++ b/src/general.h
@@ -64,7 +64,7 @@
 /** @brief Constant defines the size by which vertex_list expands at once, so expansion is not needed each time new data arrives. Also
  * vlist will be decreased by same steps.
  */
-#define TS_VLIST_CHUNK 6
+#define TS_VLIST_CHUNK 7
 
 /** For the purpose of greater flexibility all data types used in the program
  *  shouldn't use standard C types, but should use types defined here.
diff --git a/src/vertex.c b/src/vertex.c
index 653d8d2..871fe29 100644
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -22,9 +22,11 @@
     for(i=0;i<N;i++) {
         vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex));
         vlist->vtx[i]->idx=i;
+	vlist->vtx[i]->neigh=init_vertex_list(0);
     	/* initialize Ylm for spherical hamonics DONE in sh.c */
     	}
     vlist->n=N;
+	vlist->list_size=TS_VLIST_CHUNK; //TODO: can be buggy in some cases, when N>0 and we want to delete some vertices.
 	return vlist;
 }
 
@@ -32,7 +34,12 @@
 ts_bool vertex_list_add_vtx(ts_vertex_list *vlist, ts_vertex *vtx){
 
 #ifdef DEBUG
-	if(vtx==NULL || vlist==NULL) return TS_FAIL;
+	if(vtx==NULL)
+	err("VTX is null");
+	if(vlist==NULL) err("VLIST is null");
+
+	if(vtx==NULL || vlist==NULL)
+		return TS_FAIL;
 #endif
 	if(vlist->list_size < vlist->n+1){
 		vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size+TS_VLIST_CHUNK)*sizeof(ts_vertex*));
@@ -41,6 +48,7 @@
 		}
 		vlist->list_size+=TS_VLIST_CHUNK;
 	}
+//	printf("Test %u (max %u)!\n", vlist->n, vlist->list_size);
 	vlist->vtx[vlist->n]=vtx;
 	vlist->n++;
 	return TS_SUCCESS;
@@ -93,18 +101,26 @@
 
 
 ts_bool vtx_add_neighbour(ts_vertex *vtx1, ts_vertex *vtx2){
-    	vertex_list_add_vtx(vtx1->neigh, vtx2);
-    	vertex_list_add_vtx(vtx2->neigh, vtx1);
+	ts_uint i;
+	if(vtx1==NULL || vtx2==NULL) return TS_FAIL;
+	for(i=0;i<vtx1->neigh->n;i++){
+		if (vtx1->neigh->vtx[i]==vtx2){
+			return TS_FAIL;
+		}
+	}
+		vertex_list_add_vtx(vtx1->neigh, vtx2);
 	return TS_SUCCESS;
 }
 
 ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
     ts_bool retval;
-    retval=vertex_list_add_vtx(vtx1->neigh, vtx2);
-    retval=vertex_list_add_vtx(vtx2->neigh, vtx1);
-    if(retval==TS_SUCCESS)
-	    retval=vtx_add_bond(blist,vtx1,vtx2); 
-
+	retval=vtx_add_neighbour(vtx1, vtx2);
+//    retval=vertex_list_add_vtx(vtx1->neigh, vtx2);
+//    retval=vertex_list_add_vtx(vtx2->neigh, vtx1);
+    if(retval==TS_SUCCESS){
+	//	fprintf(stderr,"Bond added\n");
+	   retval=vtx_add_bond(blist,vtx1,vtx2); 
+		}
     return retval;
 }
 
@@ -120,7 +136,10 @@
 
 
 ts_bool vtx_free(ts_vertex  *vtx){
-    if(vtx->neigh!=NULL)   free(vtx->neigh);
+    if(vtx->neigh!=NULL)  {
+		if (vtx->neigh->vtx!=NULL) free(vtx->neigh->vtx);
+		free(vtx->neigh);
+    }
     if(vtx->tristar!=NULL) free(vtx->tristar);
     if(vtx->bond!=NULL)    free(vtx->bond);
     free(vtx);
@@ -216,7 +235,7 @@
 
 ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){
     memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex));
-    cvtx->neigh=NULL;
+    cvtx->neigh=init_vertex_list(0);
     cvtx->tristar_no=0;
     cvtx->bond_no=0;
     cvtx->tristar=NULL;

--
Gitblit v1.9.3