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