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