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