Debian Codegraph @

3rdparty/mshmet/src/sourceslib/mshme1.c

raw
1 #include "mshmet.h"
2
3 #define SQRT3DIV2 0.8660254037844386
4
5
6 /* control mesh gradation (aniso) */
7 static int hgrada_3d(pMesh mesh,pSol sol) {
8 pPoint p1,p2;
9 pHash hash;
10 hedge *pht;
11 double ux,uy,uz,dd,d1,d2,dh,dd1,dd2,ma1[6],mb1[6];
12 double logh,tail,rap,logs,coef,coef1,coef2;
13 double *ma,*mb;
14 int i,k,a,b,nc,nbc,it,maxt;
15
16 /* reset color */
17 for (k=1; k<=mesh->np; k++)
18 mesh->point[k].mark = mesh->mark;
19
20 /* build edge list */
21 hash = hashEdge_3d(mesh);
22 assert(hash);
23
24 /* default values */
25 logh = log(mesh->info.hgrad);
26 logs = 0.001 + logh;
27 it = 0;
28 nbc = 0;
29 maxt = 100;
30
31 /* scan edge table */
32 do {
33 mesh->mark++;
34 nc = 0;
35 for (k=1; k<=hash->hnxt; k++) {
36 pht = &hash->item[k];
37 if ( !pht->min ) continue;
38 a = pht->min;
39 b = pht->max;
40 p1 = &mesh->point[a];
41 p2 = &mesh->point[b];
42 if ( p1->mark < mesh->mark-1 && p1->mark < mesh->mark-1 ) continue;
43
44 ma = &sol->met[6*(a-1)+1];
45 mb = &sol->met[6*(b-1)+1];
46
47 /* compute edge lengths */
48 ux = p2->c[0] - p1->c[0];
49 uy = p2->c[1] - p1->c[1];
50 uz = p2->c[2] - p1->c[2];
51 d1 = ma[0]*ux*ux + ma[3]*uy*uy + ma[5]*uz*uz \
52 + 2.0*(ma[1]*ux*uy + ma[2]*ux*uz + ma[4]*uy*uz);
53 if ( d1 <= EPS1 ) d1 = EPS1;
54 dd1 = MS_MAX(EPS1,sqrt(d1));
55 d2 = mb[0]*ux*ux + mb[3]*uy*uy + mb[5]*uz*uz \
56 + 2.0*(mb[1]*ux*uy + mb[2]*ux*uz + mb[4]*uy*uz);
57 if ( d2 <= EPS1 ) d2 = EPS1;
58 dd2 = MS_MAX(EPS1,sqrt(d2));
59
60 /* swap vertices */
61 if ( dd1 > dd2 ) {
62 p1 = &mesh->point[b];
63 p2 = &mesh->point[a];
64 ma = &sol->met[6*(b-1)+1];
65 mb = &sol->met[6*(a-1)+1];
66 dd = dd1;
67 dd1 = dd2;
68 dd2 = dd;
69 }
70
71 /* check edge ratio */
72 rap = dd2 / dd1;
73 dh = rap - 1.0;
74 if ( fabs(dh) > EPS1 ) {
75 tail = (dd1+dd2+4*sqrt(0.5*(d1+d2))) / 6.0;
76 coef = log(rap) / tail;
77
78 /* update sizes */
79 if ( coef > logs ) {
80 coef = exp(tail*logh);
81 p1->mark = mesh->mark;
82 p2->mark = mesh->mark;
83
84 coef1 = 1.0 + logh*dd2;
85 coef1 = 1.0 / (coef1*coef1);
86 coef2 = 1.0 + logh*dd1;
87 coef2 = 1.0 / (coef2*coef2);
88
89 /* metric intersection */
90 for (i=0; i<6; i++) {
91 ma1[i] = coef2 * ma[i];
92 mb1[i] = coef1 * mb[i];
93 }
94 if ( !redsim_3d(ma,mb1,ma) )
95 for (i=0; i<6; i++) ma[i] = SQRT3DIV2 * (ma[i]+mb1[i]);
96 if ( !redsim_3d(ma1,mb,mb) )
97 for (i=0; i<6; i++) mb[i] = SQRT3DIV2 * (mb[i]+ma1[i]);
98 nc++;
99 }
100 }
101 }
102 nbc += nc;
103 if ( nc > 0 ) fprintf(stdout," -- %d sizes adapted\n",nc);
104 }
105 while ( nc > 0 && ++it <= maxt );
106
107 return(1);
108 }
109
110
111 /* control mesh gradation (iso) */
112 static int hgradi_3d(pMesh mesh,pSol sol) {
113 pPoint p1,p2;
114 pHash hash;
115 hedge *pht;
116 double ux,uy,uz,dd,dh;
117 double logh,tail,rap,logs,lograp,coef;
118 double *ma,*mb;
119 int k,a,b,nc,nbc,it,maxt;
120
121 /* reset color */
122 mesh->mark = 0;
123 for (k=1; k<=mesh->np; k++)
124 mesh->point[k].mark = mesh->mark;
125
126 /* build edge list */
127 hash = hashEdge_3d(mesh);
128 assert(hash);
129
130 /* default values */
131 logh = log(mesh->info.hgrad);
132 logs = 0.001 + logh;
133 it = 0;
134 nbc = 0;
135 maxt = 100;
136
137 /* scan edge table */
138 do {
139 mesh->mark++;
140 nc = 0;
141 for (k=1; k<=hash->hnxt; k++) {
142 pht = &hash->item[k];
143 if ( !pht->min ) continue;
144 a = pht->min;
145 b = pht->max;
146 p1 = &mesh->point[a];
147 p2 = &mesh->point[b];
148 if ( p1->mark < mesh->mark-1 && p1->mark < mesh->mark-1 ) continue;
149
150 /* compute edge lengths */
151 ux = p2->c[0] - p1->c[0];
152 uy = p2->c[1] - p1->c[1];
153 uz = p2->c[2] - p1->c[2];
154 dd = sqrt(ux*ux + uy*uy + uz*uz);
155 ma = &sol->met[a];
156 mb = &sol->met[b];
157 if ( ma[0] > mb[0] ) {
158 p1 = &mesh->point[b];
159 p2 = &mesh->point[a];
160 ma = &sol->met[b];
161 mb = &sol->met[a];
162 }
163 rap = mb[0] / ma[0];
164 dh = rap - 1.0;
165 if ( fabs(dh) > EPS ) {
166 lograp = log(rap);
167 tail = dd * dh / (mb[0]*lograp);
168 coef = lograp / tail;
169 /* update sizes */
170 if ( coef > logs ) {
171 p2->mark = mesh->mark;
172 mb[0] = ma[0] * exp(tail*logh);
173 nc++;
174 }
175 }
176 }
177 nbc += nc;
178 if ( nc > 0 ) fprintf(stdout," -- %d sizes adapted\n",nc);
179 }
180 while ( nc > 0 && ++it <= maxt );
181
182 return(1);
183 }
184
185
186 /* control mesh gradation (aniso) */
187 static int hgrada_2d(pMesh mesh,pSol sol) {
188 pPoint p1,p2;
189 pHash hash;
190 hedge *pht;
191 double ux,uy,dd,d1,d2,dh,dd1,dd2,ma1[6],mb1[6];
192 double logh,tail,rap,logs,coef,coef1,coef2;
193 double *ma,*mb;
194 int i,k,a,b,nc,nbc,it,maxt;
195
196 /* reset color */
197 for (k=1; k<=mesh->np; k++)
198 mesh->point[k].mark = mesh->mark;
199
200 /* build edge list */
201 hash = hashEdge_2d(mesh);
202 assert(hash);
203
204 /* default values */
205 logh = log(mesh->info.hgrad);
206 logs = 0.001 + logh;
207 it = 0;
208 nbc = 0;
209 maxt = 100;
210
211 /* scan edge table */
212 do {
213 mesh->mark++;
214 nc = 0;
215 for (k=1; k<=hash->hnxt; k++) {
216 pht = &hash->item[k];
217 if ( !pht->min ) continue;
218 a = pht->min;
219 b = pht->max;
220 p1 = &mesh->point[a];
221 p2 = &mesh->point[b];
222 if ( p1->mark < mesh->mark-1 && p1->mark < mesh->mark-1 ) continue;
223
224 ma = &sol->met[3*(a-1)+1];
225 mb = &sol->met[3*(b-1)+1];
226
227 /* compute edge lengths */
228 ux = p2->c[0] - p1->c[0];
229 uy = p2->c[1] - p1->c[1];
230 d1 = ma[0]*ux*ux + ma[2]*uy*uy + 2.0*ma[1]*ux*uy;
231 if ( d1 <= EPS1 ) d1 = EPS1;
232 dd1 = MS_MAX(EPS1,sqrt(d1));
233 d2 = mb[0]*ux*ux + mb[2]*uy*uy + 2.0*mb[1]*ux*uy;
234 if ( d2 <= EPS1 ) d2 = EPS1;
235 dd2 = MS_MAX(EPS1,sqrt(d2));
236
237 /* swap vertices */
238 if ( dd1 > dd2 ) {
239 p1 = &mesh->point[b];
240 p2 = &mesh->point[a];
241 ma = &sol->met[3*(b-1)+1];
242 mb = &sol->met[3*(a-1)+1];
243 dd = dd1;
244 dd1 = dd2;
245 dd2 = dd;
246 }
247
248 /* check edge ratio */
249 rap = dd2 / dd1;
250 dh = rap - 1.0;
251 if ( fabs(dh) > EPS1 ) {
252 tail = (dd1+dd2+4*sqrt(0.5*(d1+d2))) / 6.0;
253 coef = log(rap) / tail;
254
255 /* update sizes */
256 if ( coef > logs ) {
257 coef = exp(tail*logh);
258 p1->mark = mesh->mark;
259 p2->mark = mesh->mark;
260
261 coef1 = 1.0 + logh*dd2;
262 coef1 = 1.0 / (coef1*coef1);
263 coef2 = 1.0 + logh*dd1;
264 coef2 = 1.0 / (coef2*coef2);
265
266 /* metric intersection */
267 for (i=0; i<6; i++) {
268 ma1[i] = coef2 * ma[i];
269 mb1[i] = coef1 * mb[i];
270 }
271 if ( !redsim_2d(ma,mb1,ma) )
272 for (i=0; i<3; i++) ma[i] = SQRT3DIV2 * (ma[i]+mb1[i]);
273 if ( !redsim_2d(ma1,mb,mb) )
274 for (i=0; i<3; i++) mb[i] = SQRT3DIV2 * (mb[i]+ma1[i]);
275 nc++;
276 }
277 }
278
279 }
280 nbc += nc;
281 if ( nc > 0 ) fprintf(stdout," -- %d sizes adapted\n",nc);
282 }
283 while ( nc > 0 && ++it <= maxt );
284
285 return(1);
286 }
287
288
289 /* control mesh gradation (aniso) */
290 static int hgradi_2d(pMesh mesh,pSol sol) {
291 pPoint p1,p2;
292 pHash hash;
293 hedge *pht;
294 double ux,uy,dd,dh;
295 double logh,tail,rap,logs,lograp,coef;
296 double *ma,*mb;
297 int k,a,b,nc,nbc,it,maxt;
298
299 /* reset color */
300 for (k=1; k<=mesh->np; k++)
301 mesh->point[k].mark = mesh->mark;
302
303 /* build edge list */
304 hash = hashEdge_2d(mesh);
305 assert(hash);
306
307 /* default values */
308 logh = log(mesh->info.hgrad);
309 logs = 0.001 + logh;
310 it = 0;
311 nbc = 0;
312 maxt = 100;
313
314 /* scan edge table */
315 do {
316 mesh->mark++;
317 nc = 0;
318 for (k=1; k<=hash->hnxt; k++) {
319 pht = &hash->item[k];
320 if ( !pht->min ) continue;
321 a = pht->min;
322 b = pht->max;
323 p1 = &mesh->point[a];
324 p2 = &mesh->point[b];
325 if ( p1->mark < mesh->mark-1 && p1->mark < mesh->mark-1 ) continue;
326
327 /* compute edge lengths */
328 ux = p2->c[0] - p1->c[0];
329 uy = p2->c[1] - p1->c[1];
330 dd = sqrt(ux*ux + uy*uy);
331 ma = &sol->met[a];
332 mb = &sol->met[b];
333 if ( ma[0] > mb[0] ) {
334 ma = &sol->met[b];
335 mb = &sol->met[a];
336 p1 = &mesh->point[b];
337 p2 = &mesh->point[a];
338 }
339 rap = mb[0] / ma[0];
340 dh = rap - 1.0;
341 if ( fabs(dh) > EPS ) {
342 lograp = log(rap);
343 tail = dd * dh / (mb[0]*lograp);
344 coef = lograp / tail;
345 /* update sizes */
346 if ( coef > logs ) {
347 p1->mark = mesh->mark;
348 p2->mark = mesh->mark;
349 mb[0] = ma[0] * exp(tail*logh);
350 nc++;
351 }
352 }
353
354 }
355 nbc += nc;
356 if ( nc > 0 ) fprintf(stdout," -- %d sizes adapted\n",nc);
357 }
358 while ( nc > 0 && ++it <= maxt );
359
360 return(1);
361 }
362
363
364 int mshme1(pMesh mesh,pSol sol) {
365 pPoint ppt;
366 int j,k,siz,ier,it,nex,ney;
367
368 /* memory alloc */
369 sol->grd = (double*)M_calloc(sol->np+1,sol->dim*sizeof(double),"grd");
370 assert(sol->grd);
371 siz = sol->dim == 2 ? 3 : 6;
372 sol->hes = (double*)M_calloc(sol->np+1,siz*sizeof(double),"hes");
373 assert(sol->hes);
374
375 if ( mesh->info.metric == 0 ) {
376 if ( mesh->info.iso )
377 sol->met = (double*)M_calloc(sol->np+1,sizeof(double),"mshme1");
378 else {
379 siz = sol->dim == 2 ? 3 : 6;
380 sol->met = (double*)M_calloc(sol->np+1,siz*sizeof(double),"mshme1");
381 }
382 assert(sol->met);
383 }
384 if ( mesh->info.ls ) sol->type = 1;
385
386 for (j=0; j<sol->type; j++) {
387 if ( mesh->info.nsol > -1 && mesh->info.nsol != j ) continue;
388 if ( mesh->info.imprim < 0 )
389 fprintf(stdout," Solution %d: %d vertices\n",j+1,mesh->np);
390
391 /* solution smoothing */
392 if ( mesh->info.nlis ) lissag(mesh,sol,j,mesh->info.nlis);
393
394 /* compute gradient */
395 nex = ney = 0;
396 for (k=1; k<=mesh->np; k++) {
397 ier = gradLS(mesh,sol,k,j);
398 if ( !ier ) {
399 fprintf(stdout," %%%% Unable to evaluate gradient %d\n.",k);
400 return(0);
401 }
402 }
403
404 /* compute hessian */
405 for (k=1; k<=mesh->np; k++) {
406 ier = hessLS(mesh,sol,k,j);
407 if ( !ier ) return(0);
408 else if ( ier < 0 ) nex++;
409 }
410
411 /* correction */
412 if ( nex ) {
413 ney = nex;
414 it = 0;
415 do {
416 nex = 0;
417 for (k=1; k<=mesh->np; k++) {
418 ppt = &mesh->point[k];
419 if ( !ppt->h ) {
420 ier = avgval(mesh,sol,k);
421 if ( !ier ) ier = clsval(mesh,sol,k);
422 if ( !ier ) nex++;
423 }
424 }
425 }
426 while ( nex > 0 && ++it < 100 );
427 }
428
429 if ( mesh->info.imprim < 0 && ney )
430 fprintf(stdout," %%%% %d corrected %d unknowns\n",ney,nex);
431
432 /* norm + avg hessian */
433 if ( !nrmhes(mesh,sol,j) ) return(0);
434 //if ( !laplac(mesh,der) ) return(0);
435
436 /* define metric */
437 if ( mesh->info.ddebug ) outder(mesh,sol);
438 if ( mesh->info.ls )
439 break;
440 else if ( !defmet(mesh,sol,j) ) return(0);
441 }
442
443 /* levelset metric */
444 if ( mesh->info.ls )
445 if ( !metrLS(mesh,sol) ) return(0);
446
447 M_free(sol->grd);
448 M_free(sol->hes);
449 if ( sol->dim == 3 && !mesh->ne ) M_free(sol->nn);
450
451 if ( mesh->info.hgrad > 0.0 ) {
452 if ( mesh->dim == 2 )
453 ier = mesh->info.iso ? hgradi_2d(mesh,sol) : hgrada_2d(mesh,sol);
454 else
455 ier = mesh->info.iso ? hgradi_3d(mesh,sol) : hgrada_3d(mesh,sol);
456 }
457
458 return(1);
459 }