Trisurf Monte Carlo simulator
Samo Penic
2010-11-28 a2a67644115039486dc07aa4d9d636267f99b22a
triangles added to the data structure.
6 files modified
204 ■■■■ changed files
src/Makefile.am 4 ●●●● patch | view | raw | blame | history
src/general.h 2 ●●● patch | view | raw | blame | history
src/main.c 8 ●●●●● patch | view | raw | blame | history
src/triangle.c 180 ●●●● patch | view | raw | blame | history
src/triangle.h 7 ●●●●● patch | view | raw | blame | history
src/vertex.h 3 ●●●● patch | view | raw | blame | history
src/Makefile.am
@@ -1,5 +1,5 @@
trisurfdir=../
trisurf_PROGRAMS=trisurf
trisurf_SOURCES=general.c vertex.c bond.c main.c
trisurf_SOURCES=general.c vertex.c bond.c triangle.c main.c
trisurf_LDFLAGS = -lm -lconfuse
trisurf_CFLAGS = -Wall
trisurf_CFLAGS = -Wall -Werror
src/general.h
@@ -192,7 +192,7 @@
    ts_uint n;
    ts_triangle **tria;
};
typedef struct ts_triangle_list ts_triangle_list;
typedef struct ts_cell {
    ts_uint idx;
src/main.c
@@ -3,6 +3,7 @@
#include "general.h"
#include "vertex.h"
#include "bond.h"
#include "triangle.h"
//#include "io.h"
//#include "initial_timestep.h"
@@ -16,6 +17,9 @@
ts_bool retval;
ts_vertex_list *vlist=init_vertex_list(5);
ts_bond_list *blist=init_bond_list();
ts_triangle_list *tlist=init_triangle_list();
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");
@@ -30,7 +34,11 @@
vlist->vtx[0]->data->x=1.1;
bond_add(blist, vlist->vtx[1],vlist->vtx[0]);
triangle_add(tlist,vlist->vtx[1],vlist->vtx[2],vlist->vtx[3]);
triangle_add(tlist,vlist->vtx[1],vlist->vtx[2],vlist->vtx[3]);
triangle_list_free(tlist);
bond_list_free(blist);
vtx_list_free(vlist);
printf("Done.\n");
src/triangle.c
@@ -4,115 +4,108 @@
#include "triangle.h"
#include<math.h>
ts_bool init_triangle_list(ts_triangle_list *tlist){
ts_triangle_list *init_triangle_list(){
    ts_triangle_list *tlist=(ts_triangle_list *)malloc(sizeof(ts_triangle_list));
    tlist->n = 0;
    tlist->triangle=NULL;
    return TS_SUCCESS;
    tlist->tria=NULL;
    return tlist;
}
ts_bool clear_triangle_values(ts_triangle *triang,ts_uint idx){
    triang->idx=idx;
    triang->neigh_no=0;
    triang->vertex[0]=NULL;
    triang->vertex[1]=NULL;
    triang->vertex[2]=NULL;
    triang->neigh=NULL;
    triang->xnorm=0;
    triang->ynorm=0;
    triang->znorm=0;
    return TS_SUCCESS;
}
ts_bool triangle_add(ts_triangle_list *tlist, ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){
ts_triangle *triangle_add(ts_triangle_list *tlist, ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){
        if(vtx1==NULL || vtx2==NULL || vtx3==NULL){
            return NULL;
        }
        tlist->n++;
        tlist->triangle=realloc(tlist->triangle,tlist->n*sizeof(ts_triangle));
        if(tlist->triangle==NULL) fatal("Cannot reallocate memory for additional ts_triangle.",5);
    clear_triangle_values(&tlist->triangle[tlist->n-1],tlist->n-1);
        tlist->tria=(ts_triangle **)realloc(tlist->tria,tlist->n*sizeof(ts_triangle *));
        if(tlist->tria==NULL) fatal("Cannot reallocate memory for additional ts_triangle.",5);
        tlist->tria[tlist->n-1]=(ts_triangle *)calloc(1,sizeof(ts_triangle));
        if(tlist->tria[tlist->n-1]==NULL) fatal("Cannot reallocate memory for additional ts_triangle.",5);
        tlist->tria[tlist->n-1]->data=(ts_triangle_data *)calloc(1,sizeof(ts_triangle_data));
        //NOW insert vertices!
        tlist->triangle[tlist->n - 1].idx=tlist->n;
        tlist->triangle[tlist->n - 1].vertex[0]=vtx1;
        tlist->triangle[tlist->n - 1].vertex[1]=vtx2;
        tlist->triangle[tlist->n - 1].vertex[2]=vtx3;
        return TS_SUCCESS;
        tlist->tria[tlist->n - 1]->idx=tlist->n-1;
        tlist->tria[tlist->n - 1]->data->vertex[0]=vtx1;
        tlist->tria[tlist->n - 1]->data->vertex[1]=vtx2;
        tlist->tria[tlist->n - 1]->data->vertex[2]=vtx3;
        return tlist->tria[tlist->n-1];
}
ts_bool triangle_add_neighbour(ts_triangle *tria, ts_triangle *ntria){
   // int i;
  //fprintf(stderr,"Sosedi so:\n");
  //  for(i=0;i<tria->neigh_no;i++)
  //      fprintf(stderr,"\t %d\n",tria->neigh[i]);
    tria->neigh_no++;
        //We need to reallocate space! The pointer *neight must be zero if not
        //having neighbours yet (if neigh_no was 0 at thime of calling
        tria->neigh=realloc(tria->neigh,tria->neigh_no*sizeof(ts_triangle *));
        if(tria->neigh == NULL){
    if(tria==NULL || ntria==NULL) return TS_FAIL;
/*TODO: check if the neighbour already exists! Now there is no such check
 * because of the performance issue. */
    tria->data->neigh_no++;
    tria->data->neigh=realloc(tria->data->neigh,tria->data->neigh_no*sizeof(ts_triangle *));
    if(tria->data->neigh == NULL)
            fatal("Reallocation of memory failed during insertion of triangle neighbour in triangle_add_neighbour",3);
        }
    tria->neigh[tria->neigh_no-1]=ntria;
   // fprintf(stderr,"dodajamo soseda %d!\n",ntria);
  //fprintf(stderr,"Sosedi so:\n");
  //  for(i=0;i<tria->neigh_no;i++)
  //      fprintf(stderr,"\t %d\n",tria->neigh[i]);
    tria->data->neigh[tria->data->neigh_no-1]=ntria;
/* we repeat the procedure for the neighbour */
    ntria->data->neigh_no++;
    ntria->data->neigh=realloc(ntria->data->neigh,ntria->data->neigh_no*sizeof(ts_triangle *));
    if(ntria->data->neigh == NULL)
            fatal("Reallocation of memory failed during insertion of triangle neighbour in triangle_add_neighbour",3);
    ntria->data->neigh[ntria->data->neigh_no-1]=tria;
    return TS_SUCCESS;
}
ts_bool triangle_remove_neighbour(ts_triangle *tria, ts_triangle *ntria){
    ts_uint i,j=0;
/*    fprintf(stderr,"Sosedi so:\n");
    for(i=0;i<tria->neigh_no;i++)
        fprintf(stderr,"%d, ",tria->neigh[i]);
        fprintf(stderr,"\n");
  */
    for(i=0;i<tria->neigh_no;i++){
        if(tria->neigh[i]!=ntria){
            tria->neigh[j]=tria->neigh[i];
    ts_uint i,j=0;
    if(tria==NULL || ntria==NULL) return TS_FAIL;
    for(i=0;i<tria->data->neigh_no;i++){
        if(tria->data->neigh[i]!=ntria){
            tria->data->neigh[j]=tria->data->neigh[i];
            j++;
        } 
      //  else {
      //      fprintf(stderr,"was here once\n");
      //  }
    }
  //  fprintf(stderr,"Sosedi so:\n");
  //  for(i=0;i<tria->neigh_no;i++)
  //      fprintf(stderr,"\t %d\n",tria->neigh[i]);
//    fprintf(stderr,"iscemo soseda %d!\n",ntria);
    if(j==i) fatal("In triangle_remove_neighbour: Specified neighbour does not exist for given triangle",3);
         //   fprintf(stderr,"old nuber of neigh=%i\n",tria->neigh_no);
    tria->neigh_no--;
         //   fprintf(stderr,"new nuber of neigh=%i\n",tria->neigh_no);
    tria->neigh=realloc(tria->neigh,tria->neigh_no*sizeof(ts_triangle *));
    if(tria->neigh == NULL){
            fatal("Reallocation of memory failed during insertion of vertex neighbour in triangle_remove_neighbour",3);
        }
    if(j==i) {
        return TS_FAIL;
        //fatal("In triangle_remove_neighbour: Specified neighbour does not exist for given triangle",3);
    }
    tria->data->neigh_no--;
    tria->data->neigh=(ts_triangle **)realloc(tria->data->neigh,tria->data->neigh_no*sizeof(ts_triangle *));
    if(tria->data->neigh == NULL){
        fatal("Reallocation of memory failed during removal of vertex neighbour in triangle_remove_neighbour",100);
    }
/* we repeat the procedure for neighbour */
    for(i=0;i<ntria->data->neigh_no;i++){
        if(ntria->data->neigh[i]!=tria){
            ntria->data->neigh[j]=ntria->data->neigh[i];
            j++;
        }
    }
    if(j==i) {
        return TS_FAIL;
        //fatal("In triangle_remove_neighbour: Specified neighbour does not exist for given triangle",3);
    }
    ntria->data->neigh_no--;
    ntria->data->neigh=(ts_triangle **)realloc(ntria->data->neigh,ntria->data->neigh_no*sizeof(ts_triangle *));
    if(ntria->data->neigh == NULL){
        fatal("Reallocation of memory failed during removal of vertex neighbour in triangle_remove_neighbour",100);
    }
    return TS_SUCCESS;
}
ts_bool triangle_normal_vector(ts_triangle *tria){
    ts_double x21,x31,y21,y31,z21,z31,xden;
    x21=tria->vertex[1]->x - tria->vertex[0]->x;
    x31=tria->vertex[2]->x - tria->vertex[0]->x;
    y21=tria->vertex[1]->y - tria->vertex[0]->y;
    y31=tria->vertex[2]->y - tria->vertex[0]->y;
    z21=tria->vertex[1]->z - tria->vertex[0]->z;
    z31=tria->vertex[2]->z - tria->vertex[0]->z;
    x21=tria->data->vertex[1]->data->x - tria->data->vertex[0]->data->x;
    x31=tria->data->vertex[2]->data->x - tria->data->vertex[0]->data->x;
    y21=tria->data->vertex[1]->data->y - tria->data->vertex[0]->data->y;
    y31=tria->data->vertex[2]->data->y - tria->data->vertex[0]->data->y;
    z21=tria->data->vertex[1]->data->z - tria->data->vertex[0]->data->z;
    z31=tria->data->vertex[2]->data->z - tria->data->vertex[0]->data->z;
    tria->xnorm=y21*z31 - z21*y31;
    tria->ynorm=z21*x31 - x21*z31;
    tria->znorm=x21*y31 - y21*x31;
    xden=tria->xnorm*tria->xnorm + tria->ynorm*tria->ynorm + tria->znorm*tria->znorm;
    tria->data->xnorm=y21*z31 - z21*y31;
    tria->data->ynorm=z21*x31 - x21*z31;
    tria->data->znorm=x21*y31 - y21*x31;
    xden=tria->data->xnorm*tria->data->xnorm +
         tria->data->ynorm*tria->data->ynorm +
         tria->data->znorm*tria->data->znorm;
#ifdef TS_DOUBLE_DOUBLE
    xden=sqrt(xden);
#endif
@@ -122,24 +115,27 @@
#ifdef TS_DOUBLE_LONGDOUBLE
    xden=sqrtl(xden);
#endif
    tria->xnorm=tria->xnorm/xden;
    tria->ynorm=tria->ynorm/xden;
    tria->znorm=tria->znorm/xden;
    tria->data->xnorm=tria->data->xnorm/xden;
    tria->data->ynorm=tria->data->ynorm/xden;
    tria->data->znorm=tria->data->znorm/xden;
    return TS_SUCCESS;
}
ts_bool triangle_free(ts_triangle *triang){
    free(triang->neigh);
ts_bool triangle_data_free(ts_triangle_data *data){
    if(data->neigh!=NULL) free(data->neigh);
    free(data);
    return TS_SUCCESS;
}
ts_bool triangle_list_free(ts_triangle_list *tlist){
    int i;
    ts_uint i;
    for(i=0;i<tlist->n;i++){
        triangle_free(&tlist->triangle[i]);
        triangle_data_free(tlist->tria[i]->data);
        free(tlist->tria[i]);
    }
    free(tlist->triangle);
    free(tlist->tria);
    free(tlist);
    return TS_SUCCESS;
}
src/triangle.h
@@ -1,9 +1,8 @@
#ifndef _TRIANGLE_H
#define _TRIANGLE_H
ts_bool init_triangle_list(ts_triangle_list *tlist);
ts_bool clear_triangle_values(ts_triangle *triang,ts_uint idx);
ts_bool triangle_add(ts_triangle_list *tlist, ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3);
ts_triangle_list *init_triangle_list(void);
ts_triangle *triangle_add(ts_triangle_list *tlist, ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3);
/** Adds a neighbouring triangle in a list
 *    @param *tria is a pointer to the triangle, to which additional member want to be added
@@ -12,7 +11,7 @@
 */
ts_bool triangle_add_neighbour(ts_triangle *tria, ts_triangle *ntria);
ts_bool triangle_normal_vector(ts_triangle *tria);
ts_bool triangle_free(ts_triangle *triang);
ts_bool triangle_data_free(ts_triangle_data *triang);
ts_bool triangle_list_free(ts_triangle_list *tlist);
#endif
src/vertex.h
@@ -10,7 +10,7 @@
 *    @param zero_them is boolean value. 0 skip setting zeros to idx 
 *      and (x,y,z) coordinates for each points, 1 means to zero all 
 *      information on points > 1 requests zeroing of coordinates and 
 *      indexing the vertexes 0..N-1.
 *      indexing the vertices 0..N-1.
 *    @returns ts_bool value 1 on success, 0 otherwise
*/
ts_vertex_list *init_vertex_list(ts_uint N);
@@ -18,6 +18,7 @@
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_remove_neighbour(ts_vertex *vtx, ts_vertex *nvtx);
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);