Trisurf Monte Carlo simulator
Samo Penic
2014-04-30 fe5069cc3872b513f4715b0cfd303175ae80c468
Debugged vertexmove. testing bondflip.
4 files modified
91 ■■■■ changed files
src/constvol.c 65 ●●●●● patch | view | raw | blame | history
src/tape 2 ●●● patch | view | raw | blame | history
src/timestep.c 7 ●●●●● patch | view | raw | blame | history
src/vertexmove.c 17 ●●●● patch | view | raw | blame | history
src/constvol.c
@@ -12,45 +12,36 @@
ts_bool constvolume(ts_vesicle *vesicle, ts_vertex *vtx_avoid, ts_double Vol, ts_double *retEnergy, ts_vertex **vtx_moved_retval, ts_vertex **vtx_backup){
    ts_vertex *vtx_moved;
    ts_uint vtxind,i,j;
    ts_uint Ntries=20;
    ts_uint Ntries=3;
    ts_vertex *backupvtx;
    ts_double Rv, dh, dvol, voldiff, oenergy,delta_energy;
    ts_double Rv, dh, dvol, volFirst, voldiff, oenergy,delta_energy;
    backupvtx=(ts_vertex *)calloc(sizeof(ts_vertex),10);
    ts_double l0 = (1.0 + sqrt(vesicle->dmax))/2.0; //make this a global constant if necessary
    for(i=0;i<Ntries;i++){
        vtxind=rand() % vesicle->vlist->n;
        vtx_moved=vesicle->vlist->vtx[vtxind];
        /* chosen vertex must not be a nearest neighbour. TODO: probably must
         * extend search in case of bondflip */
        if(vtx_moved==vtx_avoid) continue;
        for(j=0;j<vtx_moved->neigh_no;j++){
            if(vtx_moved->neigh[j]==vtx_avoid) continue;
/*            for(k=0;k<vtx_moved->neigh[j]->neigh_no;k++){
                if(vtx_moved->neigh[j]->neigh[k]==vtx_avoid) continue;
            }
*/
        }
         
        memcpy((void *)&backupvtx[0],(void *)vtx_moved,sizeof(ts_vertex));
        //move vertex in specified direction. first try, test move!
        //move vertex in specified direction. first try, test move!
        Rv=sqrt(pow(vtx_moved->x,2)+pow(vtx_moved->y,2)+pow(vtx_moved->z,2));
        dh=2.0*Vol/(sqrt(3.0)*l0*l0);
//        fprintf(stderr,"Prej (x,y,z)=(%e,%e,%e).\n",vtx_moved->x,vtx_moved->y,vtx_moved->z);
        vtx_moved->x=vtx_moved->x*(1.0-dh/Rv);
        vtx_moved->y=vtx_moved->y*(1.0-dh/Rv);
        vtx_moved->z=vtx_moved->z*(1.0-dh/Rv);
//        fprintf(stderr,"Potem (x,y,z)=(%e,%e,%e). Vol=%e\n",vtx_moved->x,vtx_moved->y,vtx_moved->z,Vol);
        //check for constraints
          if(constvolConstraintCheck(vesicle, vtx_moved)==TS_FAIL){
            vtx_moved=memcpy((void *)vtx_moved,(void *)&backupvtx[0],sizeof(ts_vertex));
            continue;
        }
//        fprintf(stderr,"Sprejet.\n");
        // All checks OK!
            fprintf(stderr, "Step 1 success\n");
        for(j=0;j<vtx_moved->neigh_no;j++){
            memcpy((void *)&backupvtx[j+1],(void *)vtx_moved->neigh[j],sizeof(ts_vertex));
@@ -58,30 +49,35 @@
        dvol=0.0;
        for(j=0;j<vtx_moved->tristar_no;j++){
            dvol-=vtx_moved->tristar[j]->volume;
        }
        volFirst=dvol;
        for(j=0;j<vtx_moved->tristar_no;j++){
            triangle_normal_vector(vtx_moved->tristar[j]);
            dvol+=vtx_moved->tristar[j]->volume;
        }
        voldiff=dvol-Vol;
//TODO: here there is a bug. Don't know where, but preliminary success can
//happen sometimes. And when I do this checks, constant value is not achieved
//anymore
/*        voldiff=dvol-Vol;
        if(fabs(voldiff)/vesicle->volume < vesicle->tape->constvolprecision){
            //calculate energy, return change in energy...
             oenergy=vtx_moved->energy;
            energy_vertex(vtx_moved);
            delta_energy=vtx_moved->xk*(vtx_moved->energy - oenergy);
            //the same is done for neighbouring vertices
            for(i=0;i<vtx_moved->neigh_no;i++){
                oenergy=vtx_moved->neigh[i]->energy;
                energy_vertex(vtx_moved->neigh[i]);
                delta_energy+=vtx_moved->neigh[i]->xk*(vtx_moved->neigh[i]->energy-oenergy);
            for(j=0;j<vtx_moved->neigh_no;j++){
                oenergy=vtx_moved->neigh[j]->energy;
                energy_vertex(vtx_moved->neigh[j]);
                delta_energy+=vtx_moved->neigh[j]->xk*(vtx_moved->neigh[j]->energy-oenergy);
            }
            *retEnergy=delta_energy;
            *vtx_backup=backupvtx;
            *vtx_moved_retval=vtx_moved;
            fprintf(stderr, "Preliminary success\n");
            return TS_SUCCESS;
        }
            fprintf(stderr, "Step 2 success\n");
        }        */
//            fprintf(stderr, "Step 2 success\n");
        //do it again ;)
        dh=Vol*dh/dvol;
        vtx_moved=memcpy((void *)vtx_moved,(void *)&backupvtx[0],sizeof(ts_vertex));
@@ -94,41 +90,36 @@
                memcpy((void *)vtx_moved->neigh[j],(void *)&backupvtx[j+1],sizeof(ts_vertex));
            }
            vtx_moved=memcpy((void *)vtx_moved,(void *)&backupvtx[0],sizeof(ts_vertex));
            //also, restore normals
            for(j=0;j<vtx_moved->tristar_no;j++) triangle_normal_vector(vtx_moved->tristar[j]);
            continue;
        }
        dvol=0.0;
        dvol=volFirst;
        for(j=0;j<vtx_moved->tristar_no;j++){
            dvol-=vtx_moved->tristar[j]->volume;
            triangle_normal_vector(vtx_moved->tristar[j]);
            dvol+=vtx_moved->tristar[j]->volume;
        }
            fprintf(stderr, "Step 3a success voldiff=%e\n",voldiff);
        voldiff=dvol-Vol;
            fprintf(stderr, "Step 3b success voldiff=%e\n",voldiff);
        if(fabs(voldiff)/vesicle->volume < vesicle->tape->constvolprecision){
            //calculate energy, return change in energy...
//            fprintf(stderr, "Constvol success! %e\n",voldiff);
            oenergy=vtx_moved->energy;
            energy_vertex(vtx_moved);
            delta_energy=vtx_moved->xk*(vtx_moved->energy - oenergy);
            //the same is done for neighbouring vertices
            for(i=0;i<vtx_moved->neigh_no;i++){
                oenergy=vtx_moved->neigh[i]->energy;
                energy_vertex(vtx_moved->neigh[i]);
                delta_energy+=vtx_moved->neigh[i]->xk*(vtx_moved->neigh[i]->energy-oenergy);
            for(j=0;j<vtx_moved->neigh_no;j++){
                oenergy=vtx_moved->neigh[j]->energy;
                energy_vertex(vtx_moved->neigh[j]);
                delta_energy+=vtx_moved->neigh[j]->xk*(vtx_moved->neigh[j]->energy-oenergy);
            }
            *retEnergy=delta_energy;
            *vtx_backup=backupvtx;
            *vtx_moved_retval=vtx_moved;
            fprintf(stderr, "DVOL=%e\n",voldiff);
            return TS_SUCCESS;
        }        
    }
    free(backupvtx);
            fprintf(stderr, "fail\n");
    return TS_FAIL;
}
@@ -171,6 +162,8 @@
     for(j=0;j<vtx_moved->neigh_no;j++){
                memcpy((void *)vtx_moved->neigh[j],(void *)&vtx_backup[j+1],sizeof(ts_vertex));
    }
    for(j=0;j<vtx_moved->tristar_no;j++) triangle_normal_vector(vtx_moved->tristar[j]);
    free(vtx_backup);
    return TS_SUCCESS;
}
src/tape
@@ -18,7 +18,7 @@
#Constant volume constraint (0 disable constant volume, 1 enable)
constvolswitch=1
constvolprecision=1e-3
constvolprecision=1e-16
####### Polymer (brush) definitions ###########
src/timestep.c
@@ -27,6 +27,7 @@
    fprintf(fd, "Epoch OuterLoop VertexMoveSucessRate BondFlipSuccessRate Volume Area lamdba1 lambda2 lambda3 Kc(2-9) Kc(6-9) Kc(2-end) Kc(3-6)\n");
    centermass(vesicle);
    cell_occupation(vesicle);
    vesicle_volume(vesicle); //needed for constant volume at this moment
    if(start_iteration<inititer) ts_fprintf(stdout, "Starting simulation (first %d x %d MC sweeps will not be recorded on disk)\n", inititer, mcsweeps);
    for(i=start_iteration;i<inititer+iterations;i++){
        vmsr=0.0;
@@ -88,7 +89,7 @@
ts_bool single_timestep(ts_vesicle *vesicle,ts_double *vmsr, ts_double *bfsr){
    ts_bool retval;
    ts_double rnvec[3];
    ts_uint i,j;//b;
    ts_uint i,j, b;
    ts_uint vmsrcnt=0;
    for(i=0;i<vesicle->vlist->n;i++){
        rnvec[0]=drand48();
@@ -100,10 +101,10 @@
    ts_int bfsrcnt=0;
    for(i=0;i<3*vesicle->vlist->n;i++){
//    b=rand() % vesicle->blist->n;
    b=rand() % vesicle->blist->n;
        //find a bond and return a pointer to a bond...
        //call single_bondflip_timestep...
     //   retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
        retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
    retval=TS_FAIL;
    if(retval==TS_SUCCESS) bfsrcnt++;        
    }
src/vertexmove.c
@@ -98,8 +98,8 @@
    delta_energy=0;
//    fprintf(stderr,"Success for now.\n");
//    vesicle_volume(vesicle);
//    fprintf(stderr,"Volume in the beginning=%1.16e\n", vesicle->volume);
    //update the normals of triangles that share bead i.
    for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
@@ -118,17 +118,26 @@
        if(vesicle->pswitch==1) delta_energy-=vesicle->pressure*dvol;
    };
//    vesicle_volume(vesicle);
//    fprintf(stderr,"Volume before=%1.16e\n", vesicle->volume);
   if(vesicle->tape->constvolswitch == 1){
        retval=constvolume(vesicle, vtx, dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
        retval=constvolume(vesicle, vtx, -dvol, &delta_energy_cv, &constvol_vtx_moved,&constvol_vtx_backup);
        if(retval==TS_FAIL){ // if we couldn't move the vertex to assure constant volume
            vtx=memcpy((void *)vtx,(void *)&backupvtx[0],sizeof(ts_vertex));
            for(i=0;i<vtx->neigh_no;i++){
                vtx->neigh[i]=memcpy((void *)vtx->neigh[i],(void *)&backupvtx[i+1],sizeof(ts_vertex));
            }
            for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]); 
 //           fprintf(stderr,"fajlam!\n");
            return TS_FAIL;
        }
//    vesicle_volume(vesicle);
//    fprintf(stderr,"Volume after=%1.16e\n", vesicle->volume);
//    fprintf(stderr,"Volume after-dvol=%1.16e\n", vesicle->volume-dvol);
//    fprintf(stderr,"Denergy before=%e\n",delta_energy);
    delta_energy+=delta_energy_cv;
//    fprintf(stderr,"Denergy after=%e\n",delta_energy);
    }
/* No poly-bond energy for now!
    if(vtx->grafted_poly!=NULL){
@@ -160,9 +169,11 @@
    //update the normals of triangles that share bead i.
   for(i=0;i<vtx->tristar_no;i++) triangle_normal_vector(vtx->tristar[i]);
//    fprintf(stderr, "before vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
    if(vesicle->tape->constvolswitch == 1){
        constvolumerestore(constvol_vtx_moved,constvol_vtx_backup);
    }
//    fprintf(stderr, "after vtx(x,y,z)=%e,%e,%e\n",constvol_vtx_moved->x, constvol_vtx_moved->y, constvol_vtx_moved->z);
    return TS_FAIL; 
    }
}