Trisurf Monte Carlo simulator
Samo Penic
2014-12-16 6bc95bfe19012a33f185adf1476e623d98d33465
commit | author | age
88f451 1 #include<math.h>
SP 2 #include<stdlib.h>
3 #include "general.h"
4 #include "sh.h"
267db5 5 #include "io.h"
SP 6 #include <string.h>
074a17 7
fda1ab 8 /* do not use!!! */
074a17 9 ts_spharm *sph_init(ts_vertex_list *vlist, ts_uint l){
eb8605 10     ts_uint j,i;
074a17 11     ts_spharm *sph=(ts_spharm *)malloc(sizeof(ts_spharm));
SP 12
262607 13     sph->N=0;
eb8605 14     /* lets initialize Ylm for each vertex. */
SP 15     sph->Ylmi=(ts_double ***)calloc(l,sizeof(ts_double **));
5bb11d 16     for(i=0;i<l;i++){
SP 17             sph->Ylmi[i]=(ts_double **)calloc(2*i+1,sizeof(ts_double *));
18             for(j=0;j<(2*i+1);j++){
eb8605 19                 sph->Ylmi[i][j]=(ts_double *)calloc(vlist->n,sizeof(ts_double));
074a17 20             }
SP 21     }
22         
23     /* lets initialize ulm */
24     sph->ulm=(ts_double **)calloc(l,sizeof(ts_double *));
25     for(j=0;j<l;j++){
26         sph->ulm[j]=(ts_double *)calloc(2*j+1,sizeof(ts_double));
27     }
28
262607 29     /* lets initialize sum of Ulm2 */
SP 30     sph->sumUlm2=(ts_double **)calloc(l,sizeof(ts_double *));
31     for(j=0;j<l;j++){
32         sph->sumUlm2[j]=(ts_double *)calloc(2*j+1,sizeof(ts_double));
33     }
074a17 34
SP 35     /* lets initialize co */
79fc9c 36 //NOTE: C is has zero based indexing. Code is imported from fortran and to comply with original indexes we actually generate one index more. Also second dimension is 2*j+2 instead of 2*j+2. elements starting with 0 are useles and should be ignored!
SP 37     sph->co=(ts_double **)calloc(l+1,sizeof(ts_double *));
38     for(j=0;j<=l;j++){
39         sph->co[j]=(ts_double *)calloc(2*j+2,sizeof(ts_double));
074a17 40     }
SP 41
5bb11d 42     sph->l=l;   
SP 43
44     /* Calculate coefficients that will remain constant during all the simulation */ 
45    precomputeShCoeff(sph);
46     
074a17 47     return sph;
SP 48 }
49
50
eb8605 51 ts_bool sph_free(ts_spharm *sph){
SP 52     int i,j;
632960 53     if(sph==NULL) return TS_FAIL;
074a17 54     for(i=0;i<sph->l;i++){
SP 55         if(sph->ulm[i]!=NULL) free(sph->ulm[i]);
262607 56         if(sph->sumUlm2[i]!=NULL) free(sph->sumUlm2[i]);
074a17 57         if(sph->co[i]!=NULL) free(sph->co[i]);
SP 58     }
79fc9c 59         if(sph->co[sph->l]!=NULL) free(sph->co[sph->l]);
074a17 60     if(sph->co != NULL) free(sph->co);
SP 61     if(sph->ulm !=NULL) free(sph->ulm);
62
eb8605 63         if(sph->Ylmi!=NULL) {
074a17 64             for(i=0;i<sph->l;i++){
eb8605 65                 if(sph->Ylmi[i]!=NULL){
5bb11d 66                     for(j=0;j<i*2+1;j++){
eb8605 67                         if(sph->Ylmi[i][j]!=NULL) free (sph->Ylmi[i][j]);
SP 68                     }
69                     free(sph->Ylmi[i]);
70                 }
074a17 71             }
eb8605 72             free(sph->Ylmi);
074a17 73         }
eb8605 74
074a17 75     free(sph);
SP 76     return TS_SUCCESS;
77 }
78
88f451 79 /* Gives you legendre polynomials. Taken from NR, p. 254 */
3b3902 80 ts_double plgndr(ts_int l, ts_int m, ts_double x){
88f451 81     ts_double fact, pll, pmm, pmmp1, somx2;
SP 82     ts_int i,ll;
83
84 #ifdef TS_DOUBLE_DOUBLE
85     if(m<0 || m>l || fabs(x)>1.0)
86         fatal("Bad arguments in routine plgndr",1);
87 #endif
88 #ifdef TS_DOUBLE_FLOAT
89     if(m<0 || m>l || fabsf(x)>1.0)
90         fatal("Bad arguments in routine plgndr",1);
91 #endif
92 #ifdef TS_DOUBLE_LONGDOUBLE
93     if(m<0 || m>l || fabsl(x)>1.0)
94         fatal("Bad arguments in routine plgndr",1);
95 #endif
96     pmm=1.0;
97     if (m>0) {
98 #ifdef TS_DOUBLE_DOUBLE
99         somx2=sqrt((1.0-x)*(1.0+x));
100 #endif
101 #ifdef TS_DOUBLE_FLOAT
102         somx2=sqrtf((1.0-x)*(1.0+x));
103 #endif
104 #ifdef TS_DOUBLE_LONGDOUBLE
105         somx2=sqrtl((1.0-x)*(1.0+x));
106 #endif
107         fact=1.0;
108         for (i=1; i<=m;i++){
109             pmm *= -fact*somx2;
110             fact +=2.0;
111         }
112     }
113
114     if (l == m) return pmm;
115     else {
116         pmmp1=x*(2*m+1)*pmm;
117         if(l==(m+1)) return(pmmp1);
118         else {
119             pll=0; /* so it can not be uninitialized */
120             for(ll=m+2;ll<=l;ll++){
121                 pll=(x*(2*ll-1)*pmmp1-(ll+m-1)*pmm)/(ll-m);
122                 pmm=pmmp1;
123                 pmmp1=pll;
124             }
125             return(pll);
126         }
127     }
128 }
129
130
9bf6ee 131 /** @brief: Precomputes coefficients that are required for spherical harmonics computations.
523bf1 132
9bf6ee 133 */
523bf1 134 ts_bool precomputeShCoeff(ts_spharm *sph){
074a17 135     ts_int i,j,al,am;
SP 136     ts_double **co=sph->co;
79fc9c 137     for(i=1;i<=sph->l;i++){
SP 138         al=i;
074a17 139         sph->co[i][i+1]=sqrt((2.0*al+1.0)/2.0/M_PI);
79fc9c 140         for(j=1;j<=i-1;j++){
SP 141             am=j;
008026 142             sph->co[i][i+1+j]=co[i][i+j]*sqrt(1.0/(al-am+1.0)/(al+am));
074a17 143             sph->co[i][i+1-j]=co[i][i+1+j];
523bf1 144         }
79fc9c 145         co[i][2*i+1]=co[i][2*i]*sqrt(1.0/(2.0*al));
SP 146         co[i][1]=co[i][2*i+1];
074a17 147         co[i][i+1]=sqrt((2.0*al+1.0)/4.0/M_PI);
523bf1 148     }
SP 149     return TS_SUCCESS;
150
151 }
152
153
c9d07c 154 /** @brief: Computes Y(l,m,theta,fi) 
SP 155  *
156  * Function calculates Y^l_m for vertex with given (\theta, \fi) coordinates in
157  * spherical coordinate system.
158  * @param l is an ts_int argument.
159  * @param m is an ts_int argument.
160  * @param theta is ts_double argument.
161  * @param fi is a ts_double argument.
162  *
163  * (Miha's definition that is different from common definition for  factor srqt(1/(2*pi)) */
88f451 164 ts_double shY(ts_int l,ts_int m,ts_double theta,ts_double fi){
SP 165     ts_double fac1, fac2, K;
166     int i;
167
168     if(l<0 || m>l || m<-l)
169         fatal("Error using shY function!",1);
170
171     fac1=1.0;
af3bad 172     for(i=1; i<=l-abs(m);i++){
88f451 173         fac1 *= i;
SP 174     }
175     fac2=1.0;
af3bad 176     for(i=1; i<=l+abs(m);i++){
88f451 177         fac2 *= i;
SP 178     }
179
180     if(m==0){
181         K=sqrt(1.0/(2.0*M_PI));
182     }
183     else if (m>0) {
184         K=sqrt(1.0/(M_PI))*cos(m*fi);
185     } 
186     else {
187         //K=pow(-1.0,abs(m))*sqrt(1.0/(2.0*M_PI))*cos(m*fi);
188         if(abs(m)%2==0)
af3bad 189         K=sqrt(1.0/(M_PI))*cos(m*fi);
88f451 190         else
af3bad 191         K=-sqrt(1.0/(M_PI))*cos(m*fi);
88f451 192     }
SP 193     
632960 194     return K*sqrt((2.0*l+1.0)/2.0*(ts_double)(fac1/fac2))*plgndr(l,abs(m),cos(theta));    
88f451 195 }
523bf1 196
SP 197
198 /* Function transforms coordinates from cartesian to spherical coordinates
199  * (r,phi, theta). */
200 ts_bool *cart2sph(ts_coord *coord, ts_double x, ts_double y, ts_double z){
201     coord->coord_type=TS_COORD_SPHERICAL;
202 #ifdef TS_DOUBLE_DOUBLE
203     coord->e1=sqrt(x*x+y*y+z*z);
204     if(z==0) coord->e3=M_PI/2.0;
e9c87e 205     else coord->e3=atan2(sqrt(x*x+y*y),z);
523bf1 206     coord->e2=atan2(y,x);
SP 207 #endif
208 #ifdef TS_DOUBLE_FLOAT
209     coord->e1=sqrtf(x*x+y*y+z*z);
210     if(z==0) coord->e3=M_PI/2.0;
211     else coord->e3=atanf(sqrtf(x*x+y*y)/z);
212     coord->e2=atan2f(y,x);
213 #endif
214 #ifdef TS_DOUBLE_LONGDOUBLE
215     coord->e1=sqrtl(x*x+y*y+z*z);
216     if(z==0) coord->e3=M_PI/2.0;
217     else coord->e3=atanl(sqrtl(x*x+y*y)/z);
218     coord->e2=atan2l(y,x);
219 #endif
220
221     return TS_SUCCESS;
222 }
223
e9c87e 224
SP 225 ts_bool sph2cart(ts_coord *coord){
226     coord->coord_type=TS_COORD_CARTESIAN;
227     ts_double x,y,z;
228
229     x=coord->e1*cos(coord->e2)*sin(coord->e3);
230     y=coord->e1*sin(coord->e2)*sin(coord->e3);
231     z=coord->e1*cos(coord->e3);
232
233     coord->e1=x;
234     coord->e2=y;
235     coord->e3=z;
236
237     return TS_SUCCESS;
238 }
239
240
523bf1 241 /* Function returns radius of the sphere with the same volume as vesicle (r0) */
SP 242 ts_double getR0(ts_vesicle *vesicle){
243     ts_double r0;
244  #ifdef TS_DOUBLE_DOUBLE
245    r0=pow(vesicle->volume*3.0/4.0/M_PI,1.0/3.0);
246 #endif
247 #ifdef TS_DOUBLE_FLOAT
248    r0=powf(vesicle->volume*3.0/4.0/M_PI,1.0/3.0);
249 #endif
250 #ifdef TS_DOUBLE_LONGDOUBLE
251    r0=powl(vesicle->volume*3.0/4.0/M_PI,1.0/3.0);
252 #endif
253     return r0;
254 }
255
256
257 ts_bool preparationSh(ts_vesicle *vesicle, ts_double r0){
258 //TODO: before calling or during the call calculate area of each triangle! Can
259 //be also done after vertexmove and bondflip //
919555 260 //DONE: in energy calculation! //
523bf1 261     ts_uint i,j;
SP 262     ts_vertex **vtx=vesicle->vlist->vtx;
263     ts_vertex *cvtx;
264     ts_triangle *ctri;
265     ts_double centroid[3];
266     ts_double r;
267     for (i=0;  i<vesicle->vlist->n; i++){
268         cvtx=vtx[i];
269         //cvtx->projArea=4.0*M_PI/1447.0*(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z)/r0/r0;
270         cvtx->projArea=0.0;
271
272         /* go over all triangles that have a common vertex i */
273         for(j=0; j<cvtx->tristar_no; j++){
274             ctri=cvtx->tristar[j];
275             centroid[0]=(ctri->vertex[0]->x + ctri->vertex[1]->x + ctri->vertex[2]->x)/3.0;
276             centroid[1]=(ctri->vertex[0]->y + ctri->vertex[1]->y + ctri->vertex[2]->y)/3.0;
277             centroid[2]=(ctri->vertex[0]->z + ctri->vertex[1]->z + ctri->vertex[2]->z)/3.0;
278         /* calculating projArea+= area(triangle)*cos(theta) */
279 #ifdef TS_DOUBLE_DOUBLE
280             cvtx->projArea = cvtx->projArea + ctri->area*(-centroid[0]*ctri->xnorm - centroid[1]*ctri->ynorm - centroid[2]*ctri->znorm)/ sqrt(centroid[0]*centroid[0]+centroid[1]*centroid[1]+centroid[2]*centroid[2]);
281 #endif
282 #ifdef TS_DOUBLE_FLOAT
283             cvtx->projArea = cvtx->projArea + ctri->area*(-centroid[0]*ctri->xnorm - centroid[1]*ctri->ynorm - centroid[2]*ctri->znorm)/ sqrtf(centroid[0]*centroid[0]+centroid[1]*centroid[1]+centroid[2]*centroid[2]);
284 #endif
285 #ifdef TS_DOUBLE_LONGDOUBLE
286             cvtx->projArea = cvtx->projArea + ctri->area*(-centroid[0]*ctri->xnorm - centroid[1]*ctri->ynorm - centroid[2]*ctri->znorm)/ sqrtl(centroid[0]*centroid[0]+centroid[1]*centroid[1]+centroid[2]*centroid[2]);
287 #endif
288         }
289
290     cvtx->projArea=cvtx->projArea/3.0;
291         //we dont store spherical coordinates of vertex, so we have to calculate
292         //r(i) at this point.
293 #ifdef TS_DOUBLE_DOUBLE
294     r=sqrt(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z);
295 #endif
296 #ifdef TS_DOUBLE_FLOAT
297     r=sqrtf(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z);
298 #endif
299 #ifdef TS_DOUBLE_LONGDOUBLE
300     r=sqrtl(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z);
301 #endif
302     cvtx->relR=(r-r0)/r0;
3b3902 303     cvtx->solAngle=cvtx->projArea/r/r;
523bf1 304     }
SP 305     return TS_SUCCESS;
306 }
307
308
309
310 ts_bool calculateYlmi(ts_vesicle *vesicle){
3b3902 311     ts_int i,j,k;
523bf1 312     ts_spharm *sph=vesicle->sphHarmonics;
SP 313     ts_coord *coord=(ts_coord *)malloc(sizeof(ts_coord));
314     ts_double fi, theta;
79048f 315     ts_int m;
074a17 316     ts_vertex *cvtx;
523bf1 317     for(k=0;k<vesicle->vlist->n;k++){
074a17 318         cvtx=vesicle->vlist->vtx[k];
eb8605 319         sph->Ylmi[0][0][k]=sqrt(1.0/4.0/M_PI);
074a17 320         cart2sph(coord,cvtx->x, cvtx->y, cvtx->z);
523bf1 321         fi=coord->e2;
SP 322         theta=coord->e3; 
79048f 323         for(i=1; i<sph->l; i++){
523bf1 324             for(j=0;j<i;j++){
79048f 325             m=j+1;
3b3902 326 //Nastudiraj!!!!!
79048f 327                 sph->Ylmi[i][j][k]=sph->co[i][m]*cos((m-i-1)*fi)*pow(-1,m-i-1)*plgndr(i,abs(m-i-1),cos(theta));
3b3902 328         if(i==2 && j==0){
SP 329     /*    fprintf(stderr," **** vtx %d ****\n", k+1);
330         fprintf(stderr,"m-i-1 =%d\n",m-i-1);
331         fprintf(stderr,"fi =%e\n",fi);
332         fprintf(stderr,"(m-i-1)*fi =%e\n",((ts_double)(m-i-1))*fi);
333         fprintf(stderr,"-2*fi =%e\n",-2*fi);
334         fprintf(stderr,"m =%d\n",m);
335     
336         fprintf(stderr," cos(m-i-1)=%e\n",cos((m-i-1)*fi));
337         fprintf(stderr," cos(-2*fi)=%e\n",cos(-2*fi));
338         fprintf(stderr," sph->co[i][m]=%e\n",sph->co[i][m]);
339         fprintf(stderr," plgndr(i,abs(m-i-1),cos(theta))=%e\n",plgndr(i,abs(m-i-1),cos(theta)));
340 */
341         }
523bf1 342             }
3b3902 343 //Nastudiraj!!!!!
SP 344         j=i;
345         m=j+1;
346                 sph->Ylmi[i][j][k]=sph->co[i][m]*plgndr(i,0,cos(theta));
347             for(j=i+1;j<2*i+1;j++){
79048f 348             m=j+1;
3b3902 349 //Nastudiraj!!!!!
79048f 350                 sph->Ylmi[i][j][k]=sph->co[i][m]*sin((m-i-1)*fi)*plgndr(i,m-i-1,cos(theta));
523bf1 351             }
SP 352         }
353
354     }
355     free(coord);
356     return TS_SUCCESS;
357 }
358
359
360
361 ts_bool calculateUlm(ts_vesicle *vesicle){
362     ts_uint i,j,k;
363     ts_vertex *cvtx;
364     for(i=0;i<vesicle->sphHarmonics->l;i++){
8e58b4 365         for(j=0;j<2*i+1;j++) vesicle->sphHarmonics->ulm[i][j]=0.0;
523bf1 366     }
SP 367
368 //TODO: call calculateYlmi !!!
369
370
371     for(k=0;k<vesicle->vlist->n; k++){
372         cvtx=vesicle->vlist->vtx[k];
373         for(i=0;i<vesicle->sphHarmonics->l;i++){
3b3902 374             for(j=0;j<2*i+1;j++){
eb8605 375                 vesicle->sphHarmonics->ulm[i][j]+= cvtx->solAngle*cvtx->relR*vesicle->sphHarmonics->Ylmi[i][j][k];
523bf1 376             }
SP 377
378         }
379     }
380
381     return TS_SUCCESS;
382 }
262607 383
SP 384
385
386
387
388 ts_bool storeUlm2(ts_vesicle *vesicle){
389
390 ts_spharm *sph=vesicle->sphHarmonics;
391 ts_int i,j;
392 for(i=0;i<sph->l;i++){
393     for(j=0;j<2*i+1;j++){
8e58b4 394     /* DEBUG fprintf(stderr,"sph->sumUlm2[%d][%d]=%e\n",i,j,sph->ulm[i][j]* sph->ulm[i][j]); */
fda1ab 395         sph->sumUlm2Old[i][j]+=sph->ulm[i][j]* sph->ulm[i][j];
262607 396     }
SP 397 }
398     sph->N++;
399 return TS_SUCCESS;
400 }
dc77e8 401
SP 402
403 ts_bool saveAvgUlm2(ts_vesicle *vesicle){
404
405     FILE *fh;
267db5 406     char filename[10000];
SP 407     strcpy(filename, command_line_args.path);
fda1ab 408     strcat(filename, "sph2outOld.dat");
267db5 409     fh=fopen(filename, "w");
dc77e8 410     if(fh==NULL){
SP 411         err("Cannot open file %s for writing");
412         return TS_FAIL;
413     }
414
415     ts_spharm *sph=vesicle->sphHarmonics;
416     ts_int i,j;
417     fprintf(fh,"l,\tm,\tulm^2avg\n");
418     for(i=0;i<sph->l;i++){
419             for(j=0;j<2*i+1;j++){
fda1ab 420         fprintf(fh,"%d,\t%d,\t%e\n", i, j-i, sph->sumUlm2Old[i][j]/(ts_double)sph->N);
dc77e8 421
SP 422             }
423     fprintf(fh,"\n");
424     }
425     fclose(fh);
426     return TS_SUCCESS;
427 }