From a10dd5c18fbf0b6d5746827f7a9ebfc078563d4a Mon Sep 17 00:00:00 2001
From: Samo Penic <samo@andromeda>
Date: Sun, 28 Nov 2010 13:56:21 +0000
Subject: [PATCH] The vertex is complete... Bond is in first version.

---
 src/Makefile.am |    2 
 src/main.c      |   16 +++-
 src/vertex.c    |   55 ++++++++++---
 src/bond.h      |    6 
 src/bond.c      |   38 +++++++--
 src/general.h   |   64 ++++++++++-----
 src/general.c   |    6 +
 src/vertex.h    |    2 
 8 files changed, 136 insertions(+), 53 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index fe95642..c8e124d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,5 @@
 trisurfdir=../
 trisurf_PROGRAMS=trisurf
-trisurf_SOURCES=general.c vertex.c main.c
+trisurf_SOURCES=general.c vertex.c bond.c main.c
 trisurf_LDFLAGS = -lm -lconfuse
 trisurf_CFLAGS = -Wall -g
diff --git a/src/bond.c b/src/bond.c
index 492ad24..289079a 100644
--- a/src/bond.c
+++ b/src/bond.c
@@ -1,24 +1,42 @@
 #include<stdlib.h>
 #include "general.h"
-#include<stdio.h>
+#include "vertex.h"
 
-ts_bool init_bond_list(ts_bond_list *blist){
+ts_bond_list *init_bond_list(){
+    ts_bond_list *blist=(ts_bond_list *)malloc(sizeof(ts_bond_list));
 	blist->n=0;
 	blist->bond=NULL;
-	return TS_SUCCESS;
+	return blist;
 }
 
-ts_bool bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
+ts_bond  *bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
+    
+    /* no vertices must be null! */
+    if(vtx1==NULL || vtx2==NULL) return NULL;
+    /* TODO: Verify if the bond already exists... Don't do multiple bonds */
 	blist->n++;
-	blist->bond=realloc(blist->bond,blist->n*sizeof(ts_bond));
-	if(blist->bond==NULL) fatal("Cannot reallocate memory for additional *ts_bond.",5);
-	//NOW insert vertices!	
-	blist->bond[blist->n - 1].vtx1=vtx1;	
-	blist->bond[blist->n - 1].vtx2=vtx2;	
-	return TS_SUCCESS;
+	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);
+    blist->bond[blist->n-1]=(ts_bond *)malloc(sizeof(ts_bond));
+    if(blist->bond[blist->n-1]==NULL) fatal("Cannot allocate memory for additional *ts_bond.",100);
+    blist->bond[blist->n-1]->data=(ts_bond_data *)malloc(sizeof(ts_bond_data));
+    
+	//NOW insert vertices into data!	
+	blist->bond[blist->n - 1]->data->vtx1=vtx1;	
+	blist->bond[blist->n - 1]->data->vtx2=vtx2;
+
+    //Should we calculate bond length NOW?
+	
+	return blist->bond[blist->n-1];
 }
 
 ts_bool bond_list_free(ts_bond_list *blist){
+    ts_uint i;
+    for(i=0;i<blist->n;i++){
+    free(blist->bond[i]->data);
+    free(blist->bond[i]);
+    }
     free(blist->bond);
+    free(blist);
     return TS_SUCCESS;
 }
diff --git a/src/bond.h b/src/bond.h
index 4022a6c..e2be586 100644
--- a/src/bond.h
+++ b/src/bond.h
@@ -5,7 +5,7 @@
 /** Initialize bond list with zero values
  *	@param *blist is a pointer to a ts_bond_list structure
  */
-ts_bool init_bond_list(ts_bond_list *blist);
+ts_bond_list *init_bond_list();
 
 /** @brief Adds bond in the bond list
  *
@@ -18,9 +18,9 @@
  *  this is considered as fatal error and execution stops, returning error code to the operating
  *  system.
  */
-ts_bool bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2);
+ts_bond *bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2);
 
-
+ts_bool bond_list_free(ts_bond_list *blist);
 
 
 
diff --git a/src/general.c b/src/general.c
index b078f87..ed06398 100644
--- a/src/general.c
+++ b/src/general.c
@@ -3,9 +3,11 @@
 #include "general.h"
 #include<stdarg.h>
 
-ts_uint ts_fprintf(FILE *fd, char *fmt, va_list ap){
+ts_uint ts_fprintf(FILE *fd, char *fmt, ...){
 if(quiet) return TS_SUCCESS;
-fprintf(fd, fmt, ap); /* Call vprintf */
+    va_list ap;
+    va_start(ap,fmt);
+vfprintf(fd, fmt, ap); /* Call vfprintf */
 va_end(ap); /* Cleanup the va_list */
 return TS_SUCCESS;
 }
diff --git a/src/general.h b/src/general.h
index bd96c7a..1d1371e 100644
--- a/src/general.h
+++ b/src/general.h
@@ -111,24 +111,25 @@
 
 /** @brief Data structure of all data connected to a vertex
  *
- *  ts_vertex holds the data for one single point (bead, vertex) in the space. To understand how to use it
+ *  ts_vertex_data holds the data for one single point (bead, vertex). To understand how to use it
  *  here is a detailed description of the fields in the data structure. */
 struct ts_vertex_data {
-        ts_uint idx; /**< Represents index of the vertex point. Should become obsolete in C. */        
-        ts_double x; /**< The x coordinate of vertex. */        
+        ts_uint idx; /**< Represents index of the vertex point. Should become obsolete, since it is also present in ts_vertex structure. */        
+        ts_double x; /**< The x coordinate of vertex. */
         ts_double y; /**< The y coordinate of vertex. */
         ts_double z; /**< The z coordinate of vertex. */
         ts_uint neigh_no; /**< The number of neighbours. */
-        struct ts_vertex **neigh; /**< The pointer that holds neigh_no pointers to this structure. Careful when using pointers to pointers! Also developers do mistakes here.  */
-        ts_double *bond_length;
-        ts_double *bond_length_dual;
+        struct ts_vertex **neigh; /**< The pointer that holds neigh_no pointers to this structure. */
+        ts_double *bond_length; /**< Obsolete! The bond lenght is moved to ts_bond */
+        ts_double *bond_length_dual; /**< Obsolete! Bond length in dual lattice is moved to ts_bond! */
         ts_double curvature;
         ts_double energy;
         ts_double energy_h;
         ts_uint tristar_no;
-        struct ts_triangle **tristar;
-        struct ts_bond **bond;
-        struct ts_cell *cell;
+        struct ts_triangle **tristar; /**< The list of triangles this vertex belongs to. This is an array of pointers to ts_triangle structure of tristar_no length */
+        ts_uint bond_no;
+        struct ts_bond **bond; /**< Array of pointers of lenght bond_no that stores information on bonds. */
+        struct ts_cell *cell; /**< Which cell do we belong to? */
         ts_double xk;
         ts_double c;
         ts_uint id;
@@ -143,20 +144,33 @@
 
 typedef struct {
     ts_uint n;
-    ts_vertex *vtx;
+    ts_vertex **vtx;
 
 } ts_vertex_list;
 
 
-/** ts_bond is a structure that describes a bond */
+/** ts_bond_data is a structure that describes a bond */
 typedef struct {
 	ts_vertex *vtx1;
 	ts_vertex *vtx2;
     ts_double bond_length;
     ts_double bond_length_dual;
-} ts_bond;
+} ts_bond_data;
 
-struct ts_triangle {
+struct ts_bond {
+    ts_uint idx;
+    ts_bond_data *data;
+};
+typedef struct ts_bond ts_bond;
+
+struct ts_bond_list {
+    ts_uint n;
+    ts_bond **bond;
+};
+typedef struct ts_bond_list ts_bond_list;
+
+/** ts_triangle_data is a structure that describes a triangle */
+struct ts_triangle_data {
 	ts_uint idx;
 	ts_vertex *vertex[3];
 	ts_uint neigh_no;
@@ -166,7 +180,19 @@
 	ts_double znorm;
 	
 };
+typedef struct ts_triangle_data ts_triangle_data;
+
+struct ts_triangle {
+    ts_uint idx;
+    ts_triangle_data *data;
+};
 typedef struct ts_triangle ts_triangle;
+
+struct ts_triangle_list{
+    ts_uint n;
+    ts_triangle **tria;
+};
+
 
 typedef struct ts_cell {
     ts_uint idx;
@@ -175,15 +201,11 @@
 } ts_cell;
 
 typedef struct {
-	ts_vertex **vlist;
-	ts_bond **blist;
-	ts_triangle **tlist;
-    ts_cell **clist;
+	ts_vertex *vlist;
+	ts_bond *blist;
+	ts_triangle *tlist;
+    ts_cell *clist;
 	ts_uint nshell;
-    ts_uint nvertex;
-    ts_uint nbond;
-    ts_uint ntria;
-    ts_cell ncell;
     ts_double dcell;
     ts_double shift;
     ts_double max_occupancy;
diff --git a/src/main.c b/src/main.c
index d6c2854..41475a9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,6 +2,7 @@
 #include<math.h>
 #include "general.h"
 #include "vertex.h"
+#include "bond.h"
 //#include "io.h"
 //#include "initial_timestep.h"
 
@@ -14,13 +15,20 @@
 int main(int argv, char *argc[]){
 ts_bool retval;
 ts_vertex_list *vlist=init_vertex_list(5);
+ts_bond_list *blist=init_bond_list();
 
-
-retval=vtx_add_neighbour(VTX(1),VTX(0));
+retval=vtx_add_cneighbour(blist,vlist->vtx[1],vlist->vtx[0]);
 if(retval==TS_FAIL) printf("1. already a member or vertex is null!\n");
-retval=vtx_add_neighbour(VTX(0),VTX(1));
+
+retval=vtx_add_cneighbour(blist,vlist->vtx[0],vlist->vtx[1]);
 if(retval==TS_FAIL) printf("2. already a member or vertex is null!\n");
-VTX_DATA(1)->x=1.0;
+
+vlist->vtx[0]->data->x=1.0;
+vlist->vtx[0]->data->x=1.1;
+
+bond_add(blist, vlist->vtx[1],vlist->vtx[0]);
+
+bond_list_free(blist);
 vtx_list_free(vlist);
 printf("Done.\n");
 return 0; //program finished perfectly ok. We return 0.
diff --git a/src/vertex.c b/src/vertex.c
index b83f609..52a8f93 100644
--- a/src/vertex.c
+++ b/src/vertex.c
@@ -3,11 +3,13 @@
 #include<string.h>
 #include "general.h"
 #include "vertex.h"
+#include "bond.h"
 #include<stdio.h>
 
 ts_vertex_list *init_vertex_list(ts_uint N){	
 	ts_int i;
-    ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list *));
+    ts_vertex *tlist;
+    ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
     
 	if(N==0){
 		err("Initialized vertex list with zero elements. Pointer set to NULL");
@@ -16,17 +18,22 @@
 		return vlist;
 	}
 	
-    vlist->vtx=(ts_vertex *)malloc(N*sizeof(ts_vertex));
-    if(vlist->vtx==NULL)
+    vlist->vtx=(ts_vertex **)malloc(N*sizeof(ts_vertex *));
+    tlist=(ts_vertex *)malloc(N*sizeof(ts_vertex));
+    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<N;i++) vlist->vtx[i].data=init_vertex_data();
+    for(i=0;i<N;i++) {
+        vlist->vtx[i]=&tlist[i];
+        vlist->vtx[i]->data=init_vertex_data();
+        vlist->vtx[i]->idx=i;
+    }
     vlist->n=N;
 	return vlist;
 }
 
 ts_vertex_data *init_vertex_data(){
     ts_vertex_data *data;
-    data=(ts_vertex_data *)malloc(sizeof(ts_vertex_data));
+    data=(ts_vertex_data *)calloc(1,sizeof(ts_vertex_data));
     if(data==NULL)
         fatal("Fatal error reserving memory space for ts_vertex! Memory full?", 100);
     return data;
@@ -54,24 +61,46 @@
     for(i=0; i<vtx->data->neigh_no;i++){
         if(vtx->data->neigh[i]==nvtx) return TS_FAIL;
     }
-    ts_uint nn=vtx->data->neigh_no++;
+    ts_uint nn=++vtx->data->neigh_no;
     vtx->data->neigh=(ts_vertex **)realloc(vtx->data->neigh, nn*sizeof(ts_vertex *));
-    vtx->data->neigh[nn]=nvtx;
+    vtx->data->neigh[nn-1]=nvtx;
 
     /* pa se sosedu dodamo vertex */
     /*if it is already a neighbour don't add it to the list */
     for(i=0; i<nvtx->data->neigh_no;i++){
         if(nvtx->data->neigh[i]==vtx) return TS_FAIL;
     } 
-    nn=nvtx->data->neigh_no++;
+    nn=++nvtx->data->neigh_no;
     nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *));
-    nvtx->data->neigh[nn]=vtx;
+    nvtx->data->neigh[nn-1]=vtx;
 
-
-/* Ustvari bond in doloci dolzino */
 
     return TS_SUCCESS;
 }
+
+ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){
+    ts_bond *bond;
+    bond=bond_add(blist,vtx1,vtx2);
+    if(bond==NULL) return TS_FAIL;
+    vtx1->data->bond_no++;
+    vtx2->data->bond_no++;
+
+    vtx1->data->bond=(ts_bond **)realloc(vtx1->data->bond, vtx1->data->bond_no*sizeof(ts_bond *)); 
+    vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); 
+    vtx1->data->bond[vtx1->data->bond_no-1]=bond;
+    vtx2->data->bond[vtx2->data->bond_no-1]=bond;
+    return TS_SUCCESS;
+}
+
+ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){
+    ts_bool retval;
+    retval=vtx_add_neighbour(vtx1,vtx2);
+    if(retval==TS_SUCCESS)
+    retval=vtx_add_bond(blist,vtx1,vtx2); 
+    return retval;
+}
+
+
 
 
 ts_bool vtx_data_free(ts_vertex_data *data){
@@ -83,6 +112,7 @@
     return TS_SUCCESS;
 }
 
+/*not usable. can be deleted */
 ts_bool vtx_free(ts_vertex  *vtx){
     vtx_data_free(vtx->data);
     free(vtx);
@@ -92,8 +122,9 @@
 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);
+        vtx_data_free(vlist->vtx[i]->data);
     }
+    free(*(vlist->vtx));
     free(vlist->vtx);
     free(vlist);
     return TS_SUCCESS;
diff --git a/src/vertex.h b/src/vertex.h
index bceeb1c..1db3f1f 100644
--- a/src/vertex.h
+++ b/src/vertex.h
@@ -16,6 +16,8 @@
 ts_vertex_list *init_vertex_list(ts_uint N);
 ts_vertex_data *init_vertex_data(void);
 ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx);
+ts_bool vtx_add_cneighbour(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2);
+ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2);
 ts_bool vtx_data_free(ts_vertex_data *data);
 ts_bool vtx_free(ts_vertex *vtx);
 ts_bool vtx_list_free(ts_vertex_list *vlist);

--
Gitblit v1.9.3