Trisurf Monte Carlo simulator
Samo Penic
2016-09-15 3cd5f4a96d445b55da8a7e6da8509bbd7d5d41b8
src/poly.c
@@ -1,3 +1,4 @@
/* vim: set ts=4 sts=4 sw=4 noet : */
#include"general.h"
#include"poly.h"
#include<stdlib.h>
@@ -58,11 +59,10 @@
   ts_uint gvtxi;
   ts_double xnorm,ynorm,znorm,normlength;
   ts_double dphi,dh;
   ts_uint ji;
   // Grafting polymers:
   if (vlist!=NULL){
      if (n_poly > vlist->n){fatal("Number of polymers larger then numbero f vertices on a vesicle.",310);}
      if (n_poly > vlist->n){fatal("Number of polymers larger than numbero f vertices on a vesicle.",310);}
   
      while(i<n_poly){
         gvtxi = rand() % vlist->n;
@@ -83,6 +83,7 @@
   if (vlist!=NULL){
   /* Make straight grafted poylmers normal to membrane (polymer brush). Dist. between poly vertices put to 1*/
      ts_int intpoly=vesicle->tape->internal_poly;
      for (i=0;i<poly_list->n;i++){
   
         xnorm=0.0;
@@ -94,6 +95,9 @@
            znorm-=poly_list->poly[i]->grafted_vtx->tristar[j]->znorm;   
         }
         normlength=sqrt(xnorm*xnorm+ynorm*ynorm+znorm*znorm);
         if(intpoly && i%2){
            normlength=-normlength;
         }
         xnorm=xnorm/normlength;
         ynorm=ynorm/normlength;
         znorm=znorm/normlength;
@@ -108,14 +112,29 @@
   else
   {
   /* Make filaments inside the vesicle. Helix with radius... Dist. between poly vertices put to 1*/
      dphi = 2.0*asin(1.0/2.0/vesicle->R_nucleus)*1.001;
   ts_double a,R,H,tantheta,h,r,phi,A0=1.2;
      a = A0*(ts_double)vesicle->nshell;
      R = A0*((ts_double)vesicle->nshell)/(2.0*sin(M_PI/5.0));
      H = sqrt(a*a - R*R);
      tantheta = sqrt(R*R - a*a/4.0)/H;
      h = -H + sqrt(vesicle->clist->dmin_interspecies)*1.5;
      r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
      dphi = 2.0*asin(1.0/2.0/r)*1.001;
      dh = dphi/2.0/M_PI*1.001;
      phi=0.0;
      for(i=0;i<poly_list->n;i++){
         for (j=0;j<poly_list->poly[i]->vlist->n;j++){
            ji = j + i*poly_list->poly[i]->vlist->n;
            poly_list->poly[i]->vlist->vtx[j]->x = vesicle->R_nucleus*cos(ji*dphi);
            poly_list->poly[i]->vlist->vtx[j]->y = vesicle->R_nucleus*sin(ji*dphi);
            poly_list->poly[i]->vlist->vtx[j]->z = ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
            h = h + dh;
            r = (H-fabs(h))*tantheta - sqrt(vesicle->clist->dmin_interspecies)*1.5;
            dphi = 2.0*asin(1.0/2.0/r)*1.001;
            dh = dphi/2.0/M_PI*1.001;
            phi+=dphi;
            //ji = j + i*poly_list->poly[i]->vlist->n;
            poly_list->poly[i]->vlist->vtx[j]->x = r*cos(phi);
            poly_list->poly[i]->vlist->vtx[j]->y = r*sin(phi);
            poly_list->poly[i]->vlist->vtx[j]->z = h;// ji*dh - (dh*poly_list->n*poly_list->poly[i]->vlist->n/2.0);
         }
      }
   }
@@ -137,9 +156,9 @@
ts_bool poly_free(ts_poly *poly){
   if (poly->grafted_vtx!=NULL){
      poly->grafted_vtx->grafted_poly=NULL;
   }
//   if (poly->grafted_vtx!=NULL){
//      poly->grafted_vtx->grafted_poly=NULL;
//   }
   vtx_list_free(poly->vlist);
   bond_list_free(poly->blist);
   free(poly);
@@ -149,8 +168,9 @@
ts_bool poly_list_free(ts_poly_list *poly_list){
   ts_uint i;
   //fprintf(stderr,"no. of polys=%d\n", poly_list->n);
   for(i=0;i<poly_list->n;i++){
   //   fprintf(stderr,"%d poly address in mem=%ld\n",i+1,(long)&(poly_list->poly[i]));
      poly_free(poly_list->poly[i]);
   }
   free(poly_list->poly);
@@ -158,3 +178,42 @@
   
   return TS_SUCCESS;
}
ts_poly *remove_poly_with_index(ts_poly_list *poly_list, ts_uint idx){
   ts_uint i;
   ts_poly *removed_poly=poly_list->poly[idx];
   poly_list->n--; //decrease the total number of polymeres
   for(i=idx;i<poly_list->n;i++){ //move the rest of the polymeres up.
      poly_list->poly[i]=poly_list->poly[i+1];
//      poly_list->poly[idx]->idx=idx;
   }
   return removed_poly;
}
ts_bool remove_random_polymeres(ts_poly_list *poly_list, ts_uint number){
   ts_uint i, idx;
   ts_poly *poly;
   ts_poly **new_poly_array;
   if(number>poly_list->n) fatal("The number of polymeres to be removed from the list is greater than the number of total polymeres in the list",999);
   for(i=number;i>0;i--){
      idx=rand() % poly_list->n;
      poly=remove_poly_with_index(poly_list, idx);
      poly_free(poly);
   }
   printf("Addr before %ld\n", (long)poly_list->poly);
   new_poly_array=(ts_poly **)calloc(poly_list->n,sizeof(ts_poly *));
   for(i=0;i<poly_list->n;i++){
      new_poly_array[i]=poly_list->poly[i];
   }
   free(poly_list->poly);
   poly_list->poly=new_poly_array;
   printf("Addr after %ld\n", (long)poly_list->poly);
   return TS_SUCCESS;
}