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 // |
|
241 |
ts_uint i,j; |
|
242 |
ts_vertex **vtx=vesicle->vlist->vtx; |
|
243 |
ts_vertex *cvtx; |
|
244 |
ts_triangle *ctri; |
|
245 |
ts_double centroid[3]; |
|
246 |
ts_double r; |
|
247 |
for (i=0; i<vesicle->vlist->n; i++){ |
|
248 |
cvtx=vtx[i]; |
|
249 |
//cvtx->projArea=4.0*M_PI/1447.0*(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z)/r0/r0; |
|
250 |
cvtx->projArea=0.0; |
|
251 |
|
|
252 |
/* go over all triangles that have a common vertex i */ |
|
253 |
for(j=0; j<cvtx->tristar_no; j++){ |
|
254 |
ctri=cvtx->tristar[j]; |
|
255 |
centroid[0]=(ctri->vertex[0]->x + ctri->vertex[1]->x + ctri->vertex[2]->x)/3.0; |
|
256 |
centroid[1]=(ctri->vertex[0]->y + ctri->vertex[1]->y + ctri->vertex[2]->y)/3.0; |
|
257 |
centroid[2]=(ctri->vertex[0]->z + ctri->vertex[1]->z + ctri->vertex[2]->z)/3.0; |
|
258 |
/* calculating projArea+= area(triangle)*cos(theta) */ |
|
259 |
#ifdef TS_DOUBLE_DOUBLE |
|
260 |
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]); |
|
261 |
#endif |
|
262 |
#ifdef TS_DOUBLE_FLOAT |
|
263 |
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]); |
|
264 |
#endif |
|
265 |
#ifdef TS_DOUBLE_LONGDOUBLE |
|
266 |
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]); |
|
267 |
#endif |
|
268 |
} |
|
269 |
|
|
270 |
cvtx->projArea=cvtx->projArea/3.0; |
|
271 |
//we dont store spherical coordinates of vertex, so we have to calculate |
|
272 |
//r(i) at this point. |
|
273 |
#ifdef TS_DOUBLE_DOUBLE |
|
274 |
r=sqrt(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z); |
|
275 |
#endif |
|
276 |
#ifdef TS_DOUBLE_FLOAT |
|
277 |
r=sqrtf(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z); |
|
278 |
#endif |
|
279 |
#ifdef TS_DOUBLE_LONGDOUBLE |
|
280 |
r=sqrtl(cvtx->x*cvtx->x+cvtx->y*cvtx->y+cvtx->z*cvtx->z); |
|
281 |
#endif |
|
282 |
cvtx->relR=(r-r0)/r0; |
3b3902
|
283 |
cvtx->solAngle=cvtx->projArea/r/r; |
523bf1
|
284 |
} |
SP |
285 |
return TS_SUCCESS; |
|
286 |
} |
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
ts_bool calculateYlmi(ts_vesicle *vesicle){ |
3b3902
|
291 |
ts_int i,j,k; |
523bf1
|
292 |
ts_spharm *sph=vesicle->sphHarmonics; |
SP |
293 |
ts_coord *coord=(ts_coord *)malloc(sizeof(ts_coord)); |
|
294 |
ts_double fi, theta; |
79048f
|
295 |
ts_int m; |
074a17
|
296 |
ts_vertex *cvtx; |
523bf1
|
297 |
for(k=0;k<vesicle->vlist->n;k++){ |
074a17
|
298 |
cvtx=vesicle->vlist->vtx[k]; |
eb8605
|
299 |
sph->Ylmi[0][0][k]=sqrt(1.0/4.0/M_PI); |
074a17
|
300 |
cart2sph(coord,cvtx->x, cvtx->y, cvtx->z); |
523bf1
|
301 |
fi=coord->e2; |
SP |
302 |
theta=coord->e3; |
79048f
|
303 |
for(i=1; i<sph->l; i++){ |
523bf1
|
304 |
for(j=0;j<i;j++){ |
79048f
|
305 |
m=j+1; |
3b3902
|
306 |
//Nastudiraj!!!!! |
79048f
|
307 |
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
|
308 |
if(i==2 && j==0){ |
SP |
309 |
/* fprintf(stderr," **** vtx %d ****\n", k+1); |
|
310 |
fprintf(stderr,"m-i-1 =%d\n",m-i-1); |
|
311 |
fprintf(stderr,"fi =%e\n",fi); |
|
312 |
fprintf(stderr,"(m-i-1)*fi =%e\n",((ts_double)(m-i-1))*fi); |
|
313 |
fprintf(stderr,"-2*fi =%e\n",-2*fi); |
|
314 |
fprintf(stderr,"m =%d\n",m); |
|
315 |
|
|
316 |
fprintf(stderr," cos(m-i-1)=%e\n",cos((m-i-1)*fi)); |
|
317 |
fprintf(stderr," cos(-2*fi)=%e\n",cos(-2*fi)); |
|
318 |
fprintf(stderr," sph->co[i][m]=%e\n",sph->co[i][m]); |
|
319 |
fprintf(stderr," plgndr(i,abs(m-i-1),cos(theta))=%e\n",plgndr(i,abs(m-i-1),cos(theta))); |
|
320 |
*/ |
|
321 |
} |
523bf1
|
322 |
} |
3b3902
|
323 |
//Nastudiraj!!!!! |
SP |
324 |
j=i; |
|
325 |
m=j+1; |
|
326 |
sph->Ylmi[i][j][k]=sph->co[i][m]*plgndr(i,0,cos(theta)); |
|
327 |
for(j=i+1;j<2*i+1;j++){ |
79048f
|
328 |
m=j+1; |
3b3902
|
329 |
//Nastudiraj!!!!! |
79048f
|
330 |
sph->Ylmi[i][j][k]=sph->co[i][m]*sin((m-i-1)*fi)*plgndr(i,m-i-1,cos(theta)); |
523bf1
|
331 |
} |
SP |
332 |
} |
|
333 |
|
|
334 |
} |
|
335 |
free(coord); |
|
336 |
return TS_SUCCESS; |
|
337 |
} |
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
ts_bool calculateUlm(ts_vesicle *vesicle){ |
|
342 |
ts_uint i,j,k; |
|
343 |
ts_vertex *cvtx; |
|
344 |
for(i=0;i<vesicle->sphHarmonics->l;i++){ |
|
345 |
for(j=0;j<2*i;j++) vesicle->sphHarmonics->ulm[i][j]=0.0; |
|
346 |
} |
|
347 |
|
|
348 |
//TODO: call calculateYlmi !!! |
|
349 |
|
|
350 |
|
|
351 |
for(k=0;k<vesicle->vlist->n; k++){ |
|
352 |
cvtx=vesicle->vlist->vtx[k]; |
|
353 |
for(i=0;i<vesicle->sphHarmonics->l;i++){ |
3b3902
|
354 |
for(j=0;j<2*i+1;j++){ |
eb8605
|
355 |
vesicle->sphHarmonics->ulm[i][j]+= cvtx->solAngle*cvtx->relR*vesicle->sphHarmonics->Ylmi[i][j][k]; |
523bf1
|
356 |
} |
SP |
357 |
|
|
358 |
} |
|
359 |
} |
|
360 |
|
|
361 |
return TS_SUCCESS; |
|
362 |
} |
262607
|
363 |
|
SP |
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
368 |
ts_bool storeUlm2(ts_vesicle *vesicle){ |
|
369 |
|
|
370 |
ts_spharm *sph=vesicle->sphHarmonics; |
|
371 |
ts_int i,j; |
|
372 |
for(i=0;i<sph->l;i++){ |
|
373 |
for(j=0;j<2*i+1;j++){ |
|
374 |
sph->sumUlm2[i][j]+=sph->ulm[i][j]* sph->ulm[i][j]; |
|
375 |
} |
|
376 |
} |
|
377 |
sph->N++; |
|
378 |
return TS_SUCCESS; |
|
379 |
} |