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