Trisurf Monte Carlo simulator
Samo Penic
2012-07-12 dcd35081a6f08c911169aca6913fd71b88531b97
commit | author | age
aec47d 1 #include<stdlib.h>
SP 2 #include<math.h>
3 #include "general.h"
4 #include "vertex.h"
5 #include "bond.h"
6 #include "triangle.h"
7 #include "vesicle.h"
8 #include "energy.h"
9 #include "timestep.h"
10 #include "cell.h"
11 //#include "io.h"
12 #include<stdio.h>
13 #include "vertexmove.h"
1ad6d1 14 #include <string.h>
aec47d 15
SP 16 ts_bool single_verticle_timestep(ts_vesicle *vesicle,ts_vertex *vtx,ts_double
17 *rn){
18     ts_uint i;
19     ts_double dist;
20     ts_bool retval; 
21     ts_uint cellidx; 
22     ts_double delta_energy,oenergy;
1ad6d1 23     //This will hold all the information of vtx and its neighbours
dcd350 24 //    ts_vertex **backupvtx=(ts_vertex **)calloc(vtx->neigh_no+1,sizeof(ts_vertex *));
SP 25     ts_vertex backupvtx[20];
26 //    backupvtx[0]=(ts_vertex *)malloc(sizeof(ts_vertex));    
27     memcpy((void *)&backupvtx[0],(void *)vtx,sizeof(ts_vertex));
352fad 28         //temporarly moving the vertex
SP 29     vtx->x=vtx->x+vesicle->stepsize*(2.0*rn[0]-1.0);
30         vtx->y=vtx->y+vesicle->stepsize*(2.0*rn[1]-1.0);
31         vtx->z=vtx->z+vesicle->stepsize*(2.0*rn[2]-1.0);
32         //check we if some length to neighbours are too much
8f6a69 33     for(i=0;i<vtx->neigh_no;i++){
352fad 34         dist=vtx_distance_sq(vtx,vtx->neigh[i]);
8f6a69 35         if(dist<1.0 || dist>vesicle->dmax) {
dcd350 36         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
SP 37 //        free(backupvtx[0]);
38 //        free(backupvtx);
314f2d 39 //    fprintf(stderr,"Fail 1, dist=%f, vesicle->dmax=%f\n", dist, vesicle->dmax);
8f6a69 40         return TS_FAIL;
SP 41         }
aec47d 42     }
SP 43     //self avoidance check with distant vertices
352fad 44      cellidx=vertex_self_avoidance(vesicle, vtx);
aec47d 45     //check occupation number
dcd350 46      retval=cell_occupation_number_and_internal_proximity(vesicle->clist,cellidx,&backupvtx[0],vtx);
aec47d 47     if(retval==TS_FAIL){
dcd350 48         vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
SP 49 //        free(backupvtx[0]);
50 //        free(backupvtx);
314f2d 51 //    fprintf(stderr,"Fail 2\n");
aec47d 52         return TS_FAIL;
SP 53     } 
1ad6d1 54    
SP 55  
352fad 56     //if all the tests are successful, then energy for vtx and neighbours is calculated
1ad6d1 57     for(i=0;i<vtx->neigh_no;i++){
dcd350 58 //    backupvtx[i+1]=(ts_vertex *)malloc(sizeof(ts_vertex));    
SP 59     memcpy((void *)&backupvtx[i+1],(void *)vtx->neigh[i],sizeof(ts_vertex));
1ad6d1 60     }
aec47d 61
SP 62     delta_energy=0;
63     //update the normals of triangles that share bead i.
8f6a69 64     for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
aec47d 65     //energy and curvature
SP 66     energy_vertex(vtx);
dcd350 67     delta_energy=vtx->xk*(vtx->energy - (&backupvtx[0])->energy);
aec47d 68     //the same is done for neighbouring vertices
8f6a69 69     for(i=0;i<vtx->neigh_no;i++){
SP 70         oenergy=vtx->neigh[i]->energy;
71         energy_vertex(vtx->neigh[i]);
72         delta_energy+=vtx->neigh[i]->xk*(vtx->neigh[i]->energy-oenergy);
aec47d 73     }
314f2d 74 //   fprintf(stderr, "DE=%f\n",delta_energy);
aec47d 75     //MONTE CARLOOOOOOOO
SP 76     if(delta_energy>=0){
77 #ifdef TS_DOUBLE_DOUBLE
78         if(exp(-delta_energy)< drand48() )
79 #endif
80 #ifdef TS_DOUBLE_FLOAT
81         if(expf(-delta_energy)< (ts_float)drand48())
82 #endif
83 #ifdef TS_DOUBLE_LONGDOUBLE
84         if(expl(-delta_energy)< (ts_ldouble)drand48())
85 #endif
86     {
87     //not accepted, reverting changes
dcd350 88     vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
SP 89 //    free(backupvtx[0]);
1ad6d1 90     for(i=0;i<vtx->neigh_no;i++){
dcd350 91     vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
SP 92 //    free(backupvtx[i+1]);
1ad6d1 93     }
dcd350 94 //    free(backupvtx);
1ad6d1 95 //    fprintf(stderr,"Reverted\n");
SP 96     
aec47d 97     //update the normals of triangles that share bead i.
dcd350 98    for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
1ad6d1 99
aec47d 100     return TS_FAIL; 
SP 101     }
102 }
103     //END MONTE CARLOOOOOOO
104
105     //TODO: change cell occupation if necessary!
314f2d 106 //    fprintf(stderr,"Success!!\n");
dcd350 107 //    free(backupvtx[0]);
SP 108 //    for(i=0;i<vtx->neigh_no;i++){
109 //    free(backupvtx[i+1]);
110 //    }
111 //    free(backupvtx);
1ad6d1 112 //    fprintf(stderr,"Accepted\n");
aec47d 113     return TS_SUCCESS;
SP 114 }
115