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