| 1 |
/* mshmetlib.c |
| 2 |
|
| 3 |
mshmetlib(int option, ...) to use mshmet via a library |
| 4 |
* compute metric based on hessian |
| 5 |
|
| 6 |
* j.morice LJLL 2010 |
| 7 |
* Copyright (c) LJLL, 2010. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "mshmet.h" |
| 11 |
#include "compil.date" |
| 12 |
extern long verbosity; |
| 13 |
char idir[5] = {0,1,2,0,1}; |
| 14 |
mytime mshmet_ctim[TIMEMAX]; |
| 15 |
int (*boulep)(pMesh ,int ,int ,int *); |
| 16 |
int (*hashel)(pMesh ); |
| 17 |
int (*gradLS)(pMesh ,pSol ,int ,int ); |
| 18 |
int (*hessLS)(pMesh ,pSol ,int ,int ); |
| 19 |
int (*avgval)(pMesh ,pSol ,int ); |
| 20 |
int (*clsval)(pMesh ,pSol ,int ); |
| 21 |
int (*nrmhes)(pMesh ,pSol ,int ); |
| 22 |
int (*redsim)(double *,double *,double *); |
| 23 |
int (*defmet)(pMesh ,pSol ,int ); |
| 24 |
double (*getSol)(pSol ,int ,int ); |
| 25 |
int (*metrLS)(pMesh mesh,pSol ); |
| 26 |
int (*lissag)(pMesh ,pSol , int ,int ); |
| 27 |
|
| 28 |
|
| 29 |
static void mshmet_excfun(int sigid) { |
| 30 |
fprintf(stdout,"\n Unexpected error:"); fflush(stdout); |
| 31 |
switch(sigid) { |
| 32 |
case SIGABRT: |
| 33 |
fprintf(stdout," Abnormal stop\n"); exit(1); |
| 34 |
case SIGFPE: |
| 35 |
fprintf(stdout," Floating-point exception\n"); exit(1); |
| 36 |
case SIGILL: |
| 37 |
fprintf(stdout," Illegal instruction\n"); exit(1); |
| 38 |
case SIGSEGV: |
| 39 |
fprintf(stdout," Segmentation fault\n"); exit(1); |
| 40 |
case SIGTERM: |
| 41 |
case SIGINT: |
| 42 |
//fprintf(stdout," Program killed\n"); exit(1); |
| 43 |
fprintf(stdout," Abnormal end\n"); exit(1); |
| 44 |
} |
| 45 |
exit(1); |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
static void usage(char *prog) { |
| 50 |
fprintf(stdout,"\n usage: %s filein[.mesh] [solin[.sol]] [fileout.sol] -eps x -hmin y -hmax z -v -iso -norm\n",prog); |
| 51 |
|
| 52 |
fprintf(stdout,"\n** Generic options :\n"); |
| 53 |
fprintf(stdout,"-d Turn on debug mode\n"); |
| 54 |
fprintf(stdout,"-h Print this message\n"); |
| 55 |
fprintf(stdout,"-ls Build levelset metric\n"); |
| 56 |
fprintf(stdout,"-v [n] Tune level of verbosity\n"); |
| 57 |
fprintf(stdout,"-m file Use metric file\n"); |
| 58 |
|
| 59 |
fprintf(stdout,"\n** Specific options : \n"); |
| 60 |
fprintf(stdout," -eps : tolerance\n"); |
| 61 |
fprintf(stdout," -hmin: min size\n"); |
| 62 |
fprintf(stdout," -hmax: max size\n"); |
| 63 |
fprintf(stdout," -iso : isotropic\n"); |
| 64 |
fprintf(stdout," -w : relative width for LS (0<w<1)\n"); |
| 65 |
fprintf(stdout," -n[i]: normalization (level i), default=0\n"); |
| 66 |
fprintf(stdout," -s n : consider solution n (only)\n"); |
| 67 |
|
| 68 |
fprintf(stdout,"\n** DEFAULT.hmet file allows to store parameter values\n"); |
| 69 |
fprintf(stdout,"eps x\n"); |
| 70 |
fprintf(stdout,"hmin y\n"); |
| 71 |
fprintf(stdout,"hmax z\n"); |
| 72 |
fprintf(stdout,"iso\n"); |
| 73 |
exit(1); |
| 74 |
} |
| 75 |
*/ |
| 76 |
|
| 77 |
/* |
| 78 |
int parsop(pMesh mesh,pSol sol) { |
| 79 |
char *ptr,data[256],key[256]; |
| 80 |
float dummy; |
| 81 |
int i,ret; |
| 82 |
FILE *in; |
| 83 |
|
| 84 |
strcpy(data,sol->name); |
| 85 |
ptr = strstr(data,".sol"); |
| 86 |
if ( ptr ) *ptr = '\0'; |
| 87 |
strcat(data,".mhes"); |
| 88 |
|
| 89 |
in = fopen(data,"r"); |
| 90 |
if ( !in ) { |
| 91 |
strcpy(data,"DEFAULT.hmet"); |
| 92 |
in = fopen(data,"r"); |
| 93 |
if ( !in ) { |
| 94 |
if ( mesh->info.imprim < 0 ) |
| 95 |
fprintf(stdout," %%%% DEFAULT VALUES (%g %g %g)\n", |
| 96 |
mesh->info.eps,mesh->info.hmin,mesh->info.hmax); |
| 97 |
return(1); |
| 98 |
} |
| 99 |
} |
| 100 |
fprintf(stdout," %%%% %s FOUND\n",data); |
| 101 |
|
| 102 |
while ( !feof(in) ) { |
| 103 |
ret = fscanf(in,"%s",key); |
| 104 |
if ( !ret || feof(in) ) break; |
| 105 |
for (i=0; i<strlen(key); i++) key[i] = tolower(key[i]); |
| 106 |
|
| 107 |
if ( !strcmp(key,"hmin") ) { |
| 108 |
fscanf(in,"%f",&dummy); |
| 109 |
mesh->info.hmin = dummy; |
| 110 |
} |
| 111 |
else if ( !strcmp(key,"hmax") ) { |
| 112 |
fscanf(in,"%f",&dummy); |
| 113 |
mesh->info.hmax = dummy; |
| 114 |
} |
| 115 |
else if ( !strcmp(key,"eps") ) { |
| 116 |
fscanf(in,"%f",&dummy); |
| 117 |
mesh->info.eps = dummy; |
| 118 |
} |
| 119 |
else if ( !strcmp(key,"iso") ) { |
| 120 |
mesh->info.iso = 1; |
| 121 |
} |
| 122 |
else if ( !strcmp(key,"norm") ) { |
| 123 |
fscanf(in,"%d",&mesh->info.nnu); |
| 124 |
} |
| 125 |
else if ( key[0] == '#' ) { |
| 126 |
fgets(key,255,in); |
| 127 |
} |
| 128 |
else fprintf(stderr," unrecognized keyword : %s\n",key); |
| 129 |
} |
| 130 |
|
| 131 |
fclose(in); |
| 132 |
return(1); |
| 133 |
} |
| 134 |
*/ |
| 135 |
|
| 136 |
static void mshmet_stats(pMesh mesh,pSol sol) { |
| 137 |
fprintf(stdout," NUMBER OF GIVEN VERTICES %8d\n",mesh->np); |
| 138 |
if ( mesh->nt ) |
| 139 |
fprintf(stdout," NUMBER OF GIVEN TRIANGLES %8d\n",mesh->nt); |
| 140 |
if ( mesh->ne ) |
| 141 |
fprintf(stdout," NUMBER OF GIVEN TETRAHEDRA %8d\n",mesh->ne); |
| 142 |
fprintf(stdout," NUMBER OF GIVEN DATA %8d\n",sol->np); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
static void mshmet_endcod() { |
| 147 |
double ttot,ttim[TIMEMAX]; |
| 148 |
int k,call[TIMEMAX]; |
| 149 |
|
| 150 |
chrono(OFF,&mshmet_ctim[0]); |
| 151 |
for (k=0; k<TIMEMAX; k++) { |
| 152 |
call[k] = mshmet_ctim[k].call; |
| 153 |
ttim[k] = mshmet_ctim[k].call ? gttime(mshmet_ctim[k]) : 0.0; |
| 154 |
} |
| 155 |
ttot = ttim[1]+ttim[2]+ttim[3]+ttim[4]; |
| 156 |
ttim[0] = MS_MAX(ttim[0],ttot); |
| 157 |
|
| 158 |
if ( ttot > 0.01 ) { |
| 159 |
fprintf(stdout,"\n -- CPU REQUIREMENTS\n"); |
| 160 |
fprintf(stdout," in/out %8.2f %% %3d. calls, %7.2f sec/call\n", |
| 161 |
100.*ttim[1]/ttim[0],call[1],ttim[1]/(float)call[1]); |
| 162 |
fprintf(stdout," analysis %8.2f %% %3d. calls, %7.2f sec/call\n", |
| 163 |
100.*ttim[2]/ttim[0],call[2],ttim[2]/(float)call[2]); |
| 164 |
fprintf(stdout," metric %8.2f %% %3d. calls, %7.2f sec/call\n", |
| 165 |
100.*ttim[3]/ttim[0],call[3],ttim[3]/(float)call[3]); |
| 166 |
fprintf(stdout," total %8.2f %% %3d. calls, %7.2f sec/call\n", |
| 167 |
100.*ttot/ttim[0],call[0],ttot/(float)call[0]); |
| 168 |
} |
| 169 |
fprintf(stdout,"\n ELAPSED TIME %.2f SEC. (%.2f)\n",ttim[0],ttot); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/* set function pointers */ |
| 174 |
/* set function pointers */ |
| 175 |
void MSHMET_setfunc(pMesh mesh) { |
| 176 |
if ( mesh->dim == 2 ) { |
| 177 |
boulep = boulep_2d; |
| 178 |
hashel = hashel_2d; |
| 179 |
gradLS = gradLS_2d; |
| 180 |
hessLS = hessLS_2d; |
| 181 |
getSol = getSol_2d; |
| 182 |
avgval = avgval_2d; |
| 183 |
clsval = clsval_2d; |
| 184 |
nrmhes = nrmhes_2d; |
| 185 |
defmet = defmet_2d; |
| 186 |
redsim = redsim_2d; |
| 187 |
metrLS = metrLS_2d; |
| 188 |
lissag = lissag_2d; |
| 189 |
} |
| 190 |
else { |
| 191 |
if ( mesh->ne > 0 ) { /* 3d */ |
| 192 |
boulep = boulep_3d; |
| 193 |
hashel = hashel_3d; |
| 194 |
gradLS = gradLS_3d; |
| 195 |
hessLS = hessLS_3d; |
| 196 |
getSol = getSol_3d; |
| 197 |
avgval = avgval_3d; |
| 198 |
clsval = clsval_3d; |
| 199 |
nrmhes = nrmhes_3d; |
| 200 |
defmet = defmet_3d; |
| 201 |
redsim = redsim_3d; |
| 202 |
metrLS = metrLS_3d; |
| 203 |
lissag = lissag_3d; |
| 204 |
} |
| 205 |
else { /* surface mesh */ |
| 206 |
boulep = boulep_2d; |
| 207 |
hashel = hashel_2d; |
| 208 |
lissag = lissag_2d; |
| 209 |
avgval = avgval_3d; |
| 210 |
clsval = clsval_3d; |
| 211 |
nrmhes = nrmhes_3d; |
| 212 |
getSol = getSol_3d; |
| 213 |
redsim = redsim_3d; |
| 214 |
gradLS = gradLS_s; |
| 215 |
hessLS = hessLS_s; |
| 216 |
defmet = defmet_s; |
| 217 |
|
| 218 |
metrLS = metrLS_3d; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
int MSHMET_mshmet(int intopt[7], double fopt[4], pMesh mesh, pSol sol){ |
| 226 |
Info *info; |
| 227 |
if ( intopt[4] ) { |
| 228 |
fprintf(stdout," -- MSHMET, Release %s (%s) \n",MS_VER,MS_REL); |
| 229 |
fprintf(stdout," %s\n",MS_CPY); |
| 230 |
fprintf(stdout," %s\n",COMPIL); |
| 231 |
} |
| 232 |
|
| 233 |
/* trap exceptions */ |
| 234 |
signal(SIGABRT,mshmet_excfun); |
| 235 |
signal(SIGFPE,mshmet_excfun); |
| 236 |
signal(SIGILL,mshmet_excfun); |
| 237 |
signal(SIGSEGV,mshmet_excfun); |
| 238 |
signal(SIGTERM,mshmet_excfun); |
| 239 |
signal(SIGINT,mshmet_excfun); |
| 240 |
//atexit(mshmet_endcod); |
| 241 |
|
| 242 |
tminit(mshmet_ctim,TIMEMAX); |
| 243 |
chrono(ON,&mshmet_ctim[0]); |
| 244 |
chrono(ON,&mshmet_ctim[1]); |
| 245 |
/* default */ |
| 246 |
info = &mesh->info; |
| 247 |
info->hmin = (float) fopt[0]; // 0.01; |
| 248 |
info->hmax = (float) fopt[1]; // 1.0; |
| 249 |
info->eps = (float) fopt[2]; // 0.01; |
| 250 |
info->width = (float) fopt[3]; // 0.05; |
| 251 |
|
| 252 |
info->nnu = intopt[0]; // 0; |
| 253 |
info->iso = intopt[1]; // 0; |
| 254 |
info->ls = intopt[2]; // 0; |
| 255 |
info->ddebug = intopt[3]; // 0; |
| 256 |
info->imprim = intopt[4]; // 10; |
| 257 |
info->nlis = intopt[5]; // 0; |
| 258 |
|
| 259 |
info->bin = 1; // pas besoin c'est pour le fichier |
| 260 |
info->nsol = -1; //-1; // pas besoin ==> on peut prendre plusieurs solutions en meme temps ??? |
| 261 |
info->metric = intopt[6]; // 0; // metric given besoin ??? |
| 262 |
|
| 263 |
MSHMET_setfunc(mesh); |
| 264 |
chrono(OFF,&mshmet_ctim[1]); |
| 265 |
if ( mesh->info.imprim ) { |
| 266 |
mshmet_stats(mesh,sol); |
| 267 |
fprintf(stdout," -- DATA READING COMPLETED. %.2f sec.\n",gttime(mshmet_ctim[1])); |
| 268 |
|
| 269 |
fprintf(stdout,"\n %s\n MODULE MSHMET-LJLL : %s (%s)\n %s\n", |
| 270 |
MS_STR,MS_VER,MS_REL,MS_STR); |
| 271 |
} |
| 272 |
|
| 273 |
/* analysis */ |
| 274 |
chrono(ON,&mshmet_ctim[2]); |
| 275 |
if ( mesh->info.imprim ) fprintf(stdout,"\n -- PHASE 1 : ANALYSIS\n"); |
| 276 |
if ( abs(mesh->info.imprim) > 4 ) { |
| 277 |
fprintf(stdout," ** SETTING ADJACENCIES\n"); |
| 278 |
fflush(stdout); |
| 279 |
} |
| 280 |
if ( !scaleMesh(mesh,sol) ) return(1); |
| 281 |
if ( !hashel(mesh) ) exit(1); |
| 282 |
chrono(OFF,&mshmet_ctim[2]); |
| 283 |
if ( mesh->info.imprim ) |
| 284 |
fprintf(stdout," -- PHASE 1 COMPLETED. %.2f sec.\n",gttime(mshmet_ctim[2])); |
| 285 |
|
| 286 |
/* metric */ |
| 287 |
chrono(ON,&mshmet_ctim[3]); |
| 288 |
if ( mesh->info.imprim ) fprintf(stdout,"\n -- PHASE 2 : METRIC\n"); |
| 289 |
if ( !mshme1(mesh,sol) ) exit(1); |
| 290 |
|
| 291 |
chrono(OFF,&mshmet_ctim[3]); |
| 292 |
if ( mesh->info.imprim ) |
| 293 |
fprintf(stdout," -- PHASE 2 COMPLETED. %.2f sec.\n",gttime(mshmet_ctim[3])); |
| 294 |
|
| 295 |
if ( mesh->info.imprim ) |
| 296 |
fprintf(stdout,"\n %s\n END OF MODULE MSHMET \n %s\n",MS_STR,MS_STR); |
| 297 |
/* |
| 298 |
sol->outn="zzzz"; |
| 299 |
if ( !saveMet(sol,&mesh->info,sol->outn) ) exit(1); |
| 300 |
*/ |
| 301 |
if ( mesh->info.imprim ) { |
| 302 |
mshmet_endcod(); |
| 303 |
|
| 304 |
fprintf(stdout,"\n %s\n END OF MODULE MSHMET \n %s\n",MS_STR,MS_STR); |
| 305 |
} |
| 306 |
if ( mesh->info.imprim < -4 || mesh->info.ddebug ) M_memDump(); |
| 307 |
|
| 308 |
return(0); |
| 309 |
} |