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