Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/Meshers/ExtrudeMesh.c
3196 views
1
//
2
// ExtrudeMesh - extrudes 2d mesh on a constant number of levels and
3
// interpolates lower and upper surface from values
4
// given by a DEM
5
//
6
//
7
// Authors: thomas Zwinger, Thorsten Malm
8
// Email: [email protected]
9
// Address: CSC - IT Center for Science Ltd.
10
// Keilaranta 14
11
// 02101 Espoo, Finland
12
//
13
// This program is free software; you can redistribute it and/or
14
// modify it under the terms of the GNU General Public License
15
// as published by the Free Software Foundation; either version 2
16
// of the License, or (at your option) any later version.
17
//
18
// This program is distributed in the hope that it will be useful,
19
// but WITHOUT ANY WARRANTY; without even the implied warranty of
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
// GNU General Public License for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with this program; if not, write to the Free Software
25
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
26
// Boston, MA 02110-1301, USA.
27
//
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <math.h>
32
#include <sys/file.h>
33
#include <sys/param.h>
34
35
#define PERMS 0666
36
#define BUF_LEN 100000
37
38
39
#ifdef WIN32
40
#include <direct.h>
41
#define MKDIR(x) _mkdir(x)
42
#else
43
#include <sys/stat.h>
44
#include <sys/types.h>
45
#define MKDIR(x) mkdir(x, S_IRWXU)
46
#endif
47
48
49
double f(double x,double GL, double dZ, double BL) {
50
return dZ - (BL+dZ)*pow(x,GL)+BL*pow(x,GL+1);
51
} /* f */
52
53
double f_prime(double x,double GL, double dZ, double BL) {
54
return -(BL+dZ)*GL*pow(x,GL-1)+BL*(GL+1)*pow(x,GL);
55
} /* f_prime */
56
57
58
59
double newton(double x_0, double tol, int max_iters,
60
int* iters_p, int* converged_p,double GL, double dZ, double BL) {
61
double x = x_0;
62
double x_prev;
63
int iter = 0;
64
65
do {
66
iter++;
67
x_prev = x;
68
x = x_prev - f(x_prev,GL,dZ,BL)/f_prime(x_prev,GL,dZ,BL);
69
} while ((fabs(x - x_prev) > tol && iter < max_iters));
70
71
if (fabs(x - x_prev) <= tol)
72
*converged_p = 1;
73
else{
74
*converged_p = 0;
75
printf("not converged, %d\n",converged_p[0]);
76
}
77
*iters_p = iter;
78
79
80
81
82
return x;
83
} /* newton algorithm */
84
85
86
87
88
89
// Compute layer coordinate Z for geometric refinement at BL and uniform:
90
//-----------------------------------------------------
91
double refinedHeightBL (double S, double B, int levels, int level, int GL, double p, double r)
92
{
93
94
95
int iters; /* Actual number of iterations */
96
int converged; /* Whether iteration converged */
97
double z;
98
99
100
int n = GL -1;
101
102
103
if(GL < 1){
104
double dZ = (S - B)/((double) levels -1);
105
z = B + level*dZ;
106
}else{
107
double BL = (S - B)*fabs(p);
108
double dZ = (S - B - BL)/((double) levels - GL -1);
109
if(r==0) r=newton(10,1e-3,1000,&iters,&converged,GL,dZ,BL);
110
//for r==0 r is calculated automatically, so that dZ=r*_last_boundary_layer_thickness_; but it's not always possible to find such an r > 1, pay attention to this!!!!
111
112
113
double a;
114
if(r>1) a = BL *(1-r)/(1-pow(r,n+1));
115
else{
116
a= BL/(GL-1);
117
printf("no good solution found, since r is equal or smaller as 1: r= %f \n",r);
118
printf("solution r %f dZ %f dZlast %f a %f\n",r,dZ,a*pow(r,GL-1),a);
119
printf("equation of the polynom for testing: poly konst %f GL %i c1 %f c2 %f \n",dZ,GL,-BL-dZ,BL);
120
}
121
122
123
int k = 0;
124
125
if(p>0){
126
z = B;
127
for(k = 0; k < level; ++k) {
128
if(k<GL){
129
z = z + a * pow(r, k);
130
}
131
else
132
z = z + dZ;
133
}
134
}else{
135
z = B;
136
for(k = 0; k < level; ++k) {
137
if(k<levels-GL-1){
138
z = z + dZ;
139
}
140
else
141
z = z + a * pow(r, levels-k-2);
142
}
143
}
144
}
145
return z;
146
}
147
148
int file_exists(const char * filename){
149
FILE * fileptr;
150
if (fileptr = fopen(filename, "r"))
151
{
152
fclose(fileptr);
153
return 1;
154
}
155
return 0;
156
}
157
158
159
int checkFileEntries(char *filename, char *argv[], int *entriesPerLine){
160
161
162
FILE *fileptr;
163
char dummys[BUF_LEN], *charptr;
164
int lines,entries;
165
166
167
fileptr = fopen(filename, "r");
168
if (fileptr==NULL){
169
printf("(%s) Error opening file %s\n",argv[0],filename);
170
return -1;
171
}
172
173
lines = 0;
174
entries = 0;
175
fgets(dummys, BUF_LEN,fileptr);
176
charptr = strtok(dummys, "\t ");
177
while(charptr != NULL) {
178
entries++;
179
charptr = strtok(NULL, "\t ");
180
}
181
while( fgets(dummys, BUF_LEN, fileptr) != NULL ){
182
lines++;
183
}
184
fclose(fileptr);
185
*entriesPerLine = entries;
186
return ++lines;
187
}
188
189
int readDEM(double *inputfield, int *isnoval, int pointsInDEM, char *filename, char *argv[]){
190
int i,validPointsInDEM;
191
double noval = atof(argv[13]);
192
FILE *fileptr;
193
194
fileptr = fopen(filename, "r");
195
if (fileptr==NULL){
196
printf("(%s) Error opening file %s\n",argv[0],filename);
197
return -1;
198
}
199
for (i=0,validPointsInDEM=0;i<pointsInDEM;++i){
200
fscanf(fileptr,"%lf %lf %lf",&inputfield[3*validPointsInDEM],&inputfield[3*validPointsInDEM+1],&inputfield[3*validPointsInDEM+2]);
201
if (inputfield[3*validPointsInDEM+2] == noval)
202
isnoval[validPointsInDEM] = 1;
203
// inputfield[3*validPointsInDEM] = inputfield[3*validPointsInDEM+1] = inputfield[3*validPointsInDEM+2] = 0.0;
204
else{
205
validPointsInDEM++;
206
isnoval[validPointsInDEM] = 0;
207
}
208
}
209
fclose(fileptr);
210
return validPointsInDEM;
211
}
212
213
int interpolatePoint(double X, double Y, double *B, double *S, double *bed, double *surf, double *thick, int *pointsInDEM, double minrad, double wexp, int interpolationScheme, char *argv[], int *isnoval0, int *isnoval1, int *isnoval2){
214
int i, j, k, rounds;
215
double weightsum, weight, radius, localthickness;
216
217
//printf("minrad=%e wexp=%e X=%e Y=%e \n",minrad,wexp, X, Y);
218
219
// do we need an interpolated thickness?
220
if (interpolationScheme > 11){
221
rounds = 0;
222
do{
223
localthickness = 0.0;
224
for (i=0,weightsum=0.0;i<pointsInDEM[2];++i){
225
radius = sqrt(pow((X-thick[3*i]),2.0) + pow((Y-thick[(3*i)+1]),2.0));
226
227
228
//printf("radius=%e, bed=%e,%e\n",radius,bed[3*i], bed[3*i+1]);
229
230
if (radius <= minrad*(1.0 + (double)(rounds+1)/100.0)){
231
if (isnoval2[i] == 1) {
232
weight = 0.0;
233
}else{
234
weight = 1.0/(pow(radius,wexp) + 1.0E-09);
235
}
236
weight = (weight > 1.0E05) ? 1.0E05 : weight;
237
weightsum += weight;
238
localthickness += weight * thick[(3*i)+2];
239
}
240
}
241
localthickness /= weightsum;
242
if (localthickness != localthickness){
243
rounds++;
244
}
245
}while ((rounds < 100) && (*B != *B));
246
247
if (rounds >= 99){
248
fprintf(stderr,"(%s) (thickness) no interpolation point within range of double the cut-off value %e\n", argv[0], minrad);
249
return EXIT_FAILURE;
250
}else if (rounds > 0){
251
fprintf(stderr,"(%s) (thickness) interpolation needed increasing cut-off value by %4e percent\n",argv[0], rounds*1.0);
252
}
253
}
254
255
256
// bedrock interpolation from DEM
257
if ((interpolationScheme == 11) || (interpolationScheme == 101)){
258
rounds = 0;
259
do{
260
*B = 0.0;
261
for (i=0,weightsum=0.0;i<pointsInDEM[0];++i){
262
radius = sqrt(pow((X-bed[3*i]),2.0) + pow((Y-bed[(3*i)+1]),2.0));
263
264
265
//printf("radius=%e, bed=%e,%e\n",radius,bed[3*i], bed[3*i+1]);
266
267
if (radius <= minrad*(1.0 + (double)(rounds+1)/100.0)){
268
if (isnoval0[i] == 1) {
269
weight = 0.0;
270
}else{
271
weight = 1.0/(pow(radius,wexp) + 1.0E-09);
272
}
273
weight = (weight > 1.0E05) ? 1.0E05 : weight;
274
weightsum += weight;
275
*B += weight * bed[(3*i)+2];
276
}
277
}
278
*B /= weightsum;
279
if (*B != *B){
280
rounds++;
281
}
282
}while ((rounds < 100) && (*B != *B));
283
284
if (rounds >= 99){
285
fprintf(stderr,"(%s) no interpolation point within range of double the cut-off value %e\n", argv[0], minrad);
286
return EXIT_FAILURE;
287
}else if (rounds > 0){
288
fprintf(stderr,"(%s) interpolation needed increasing cut-off value by %4e percent\n",argv[0], rounds*1.0);
289
}
290
}
291
292
//surface
293
if ((interpolationScheme == 11) || (interpolationScheme == 110)){
294
rounds = 0;
295
do{
296
*S=0.0;
297
for (i=0,weightsum=0.0;i<pointsInDEM[1];++i){
298
radius = sqrt(pow((X-surf[3*i]),2.0) + pow((Y-surf[(3*i)+1]),2.0));
299
if (radius <= minrad*(1.0 + (double)(rounds+1)/100.0)){
300
if (isnoval1[i] == 1) {
301
weight = 0.0;
302
}else{
303
weight = 1.0/(pow(radius,wexp) + 1.0E-09);
304
}
305
weightsum += weight;
306
*S += weight * surf[(3*i)+2];
307
}
308
}
309
if (weightsum <= 0.0){
310
rounds++;
311
}else *S /= weightsum;
312
}while ((rounds < 100) && (weightsum <= 0.0));
313
314
if (rounds >= 99){
315
fprintf(stderr,"(%s) (surface) no interpolation point within range of double the cut-off value %e\n", argv[0], minrad);
316
return EXIT_FAILURE;
317
}else if (rounds > 0){
318
fprintf(stderr,"(%s) (surface) interpolation needed increasing cut-off value by %4e percent\n",argv[0], rounds*1.0);
319
}
320
}
321
322
// get surface from bed and thickness
323
if (interpolationScheme == 101) *S = *B + localthickness;
324
// get bedrock from surface and thickness
325
if (interpolationScheme == 110) *B = *S - localthickness;
326
327
//-----
328
329
return EXIT_SUCCESS;
330
}
331
332
//----------------------------------------------------------------------------------------------
333
// extrudes every single partition
334
//---------------------------------------------------------------------------------------------------
335
int extrudepartition( char *argv[], char *inputfilename, char *outputfilename, int partition, int partitions, int levels, double depth, int pointsinlevel, int elementsinlevel, int belementsinlevel, int interpolationScheme, double *bed, double *surf, double *thick, int *pointsInDEM, double cutOffValue, double wexp, int *isnoval0, int *isnoval1, int *isnoval2)
336
{// extrudepartition
337
int i,j,k,l,level, dummyint, nodesinpartition, elementsinpartition, belementsinpartition, sharednodes, points, quads, lines, triangles, bricks, wedges, parent[2], elementtype, belementtype;
338
char instring[BUF_LEN],dummys[BUF_LEN], numberstring[BUF_LEN], dummyc, *charptr;
339
double dummydbl, dZ;
340
// arrays to be allocated
341
double *X,*Y,*S,*B;
342
int *nodeinfo, *elementinfo, *belementinfo, *sharednodeinfo;
343
char **inputfile, **outputfile;
344
FILE **outfids, **infids, *infofid;
345
346
// allocate and initiate needed arrays for input
347
//----------------------------------------------
348
infids = (FILE **) malloc(5 * sizeof(FILE *));
349
inputfile = (char **) malloc(5 * sizeof(char *));
350
for (i=0;i<5;++i)
351
inputfile[i] = (char *) malloc((MAXPATHLEN+1) * sizeof(char));
352
353
for (i=0;i<5;++i){
354
strcpy(inputfile[i],inputfilename);
355
}
356
strcat(inputfile[0],".header");//header
357
strcat(inputfile[1],".nodes");//elements
358
strcat(inputfile[2],".elements");//elements
359
strcat(inputfile[3],".boundary");//boundary
360
strcat(inputfile[4],".shared");//shared nodes
361
for (i=0;i<5;++i){
362
infids[i] = fopen(inputfile[i], "r");
363
if (infids[i] != NULL) continue;
364
printf("(%s) Could not open file %s\n",argv[0],inputfile[i]);
365
return EXIT_FAILURE;
366
}
367
// allocate and initiate needed arrays for output
368
//-----------------------------------------------
369
outfids = (FILE **) malloc(5 * sizeof(FILE *));
370
outputfile = (char **) malloc(5 * sizeof(char *));
371
for (i=0;i<5;++i)
372
outputfile[i] = (char *) malloc((MAXPATHLEN+1) * sizeof(char));
373
374
for (i=0;i<5;++i){
375
strcpy(outputfile[i],outputfilename);
376
}
377
strcat(outputfile[0],".header");//header
378
strcat(outputfile[1],".nodes");//elements
379
strcat(outputfile[2],".elements");//elements
380
strcat(outputfile[3],".boundary");//boundary
381
strcat(outputfile[4],".shared");//shared nodes
382
for (i=0;i<5;++i){
383
outfids[i] = fopen(outputfile[i], "w");
384
if (outfids[i] != NULL) continue;
385
printf("(%s) Could not open file %s\n",argv[0],outputfile[i]);
386
return EXIT_FAILURE;
387
}
388
for (i=0;i<5;++i){
389
printf("<-%s\n",inputfile[i]);
390
printf("->%s\n",outputfile[i]);
391
}
392
// read in header in order to
393
// inquire mesh sizes and open input files
394
//-----------------------------------------
395
fscanf(infids[0],"%i ",&nodesinpartition);
396
fscanf(infids[0],"%i ",&elementsinpartition);
397
fscanf(infids[0],"%i ",&belementsinpartition);
398
399
fscanf(infids[0],"%i ",&j);
400
points = lines = triangles = quads = 0;
401
for (i=0;i<j;++i){
402
fscanf(infids[0],"%i %i", &elementtype, &dummyint);
403
if (elementtype == 101) points = dummyint;
404
else if (elementtype == 202) lines = dummyint;
405
else if (elementtype == 303) triangles = dummyint;
406
else if (elementtype == 404) quads = dummyint;
407
else{
408
printf("(%s) element type %3i of input file %s not defined\n",argv[0],elementtype,inputfile[0]);
409
return EXIT_FAILURE;
410
}
411
}
412
fscanf(infids[0],"%i %i", &sharednodes, &dummyint);
413
printf("(%s) Partition %i of %i input: %i nodes, %i elements, %i boundary elements, %i shared nodes\n",argv[0], partition+1, partitions, nodesinpartition,elementsinpartition,belementsinpartition,sharednodes);
414
// now we also know the amount of wedges and bricks
415
wedges = triangles*(levels - 1); // extruded triangles
416
bricks = quads*(levels -1); // extruded quads
417
triangles *= 2; // double, as free surface and bottom
418
quads *= 2; // double, as free surface and bottom
419
quads += (levels -1)*lines; // plus the extruded lines
420
lines = (levels - 1)*points; // all extruded points
421
422
// write partition header
423
//printf("%i %i + %i %i\n",points, wedges, bricks, quads+triangles+lines);
424
fprintf(outfids[0],"%i %i %i\n",nodesinpartition*levels, wedges+bricks, quads+triangles+lines); // no. points, no- elements, no. boundary elements
425
fprintf(outfids[0],"%i\n",(quads > 0) + (triangles > 0) + (bricks > 0) + (wedges > 0) + (lines > 0) + (points > 0)); // no of different element types
426
if (lines > 0) fprintf(outfids[0],"202 %i\n", lines);
427
if (triangles > 0) fprintf(outfids[0],"303 %i\n", triangles);
428
if (quads > 0) fprintf(outfids[0],"404 %i\n", quads);
429
if (wedges > 0) fprintf(outfids[0],"706 %i\n", wedges);
430
if (bricks > 0) fprintf(outfids[0],"808 %i\n", bricks);
431
fprintf(outfids[0],"%i 0 \n",sharednodes*levels);
432
printf("(%s) Partition %i of %i output: %i nodes, %i elements, %i boundary elements, %i shared nodes\n\n",argv[0], partition+1, partitions, nodesinpartition*levels, wedges+bricks, quads+triangles+lines, sharednodes*levels);
433
434
435
X = (double *) malloc(nodesinpartition * sizeof(double));
436
Y = (double *) malloc(nodesinpartition * sizeof(double));
437
S = (double *) malloc(nodesinpartition * sizeof(double));
438
B = (double *) malloc(nodesinpartition * sizeof(double));
439
nodeinfo = (int *) malloc(nodesinpartition * sizeof(int));
440
elementinfo = (int *) malloc(elementsinpartition * 8 * sizeof(int));
441
belementinfo = (int *) malloc(belementsinpartition * 8 * sizeof(int));
442
sharednodeinfo = (int *) malloc((partitions + 2) * sizeof(int));
443
444
445
// load original mesh (footprint) nodes
446
// and compute lower as well as upper
447
// Z-coordinate
448
//-------------------------------------
449
for (i=0; i<nodesinpartition;++i){
450
fscanf(infids[1],"%i %i %le %le %le",&nodeinfo[i], &dummyint, &X[i], &Y[i], &B[i]);
451
if (interpolationScheme > 0){
452
if (interpolatePoint(X[i], Y[i], &B[i], &S[i], bed, surf, thick, pointsInDEM, cutOffValue, wexp, interpolationScheme, argv, isnoval0, isnoval1, isnoval2) == EXIT_FAILURE){
453
fprintf(stderr,"(%s) Failed interpolating values for point %i %e %e\n",argv[0],i,X[i],Y[i]);
454
return EXIT_FAILURE;
455
}else{
456
//printf("S,B= %e %e\n", B[i], S[i]);
457
458
S[i] = (S[i] <= B[i] - depth) ? (B[i] + depth) : S[i];
459
}
460
}else{
461
S[i] = B[i] + depth;
462
//printf("X(%i)= (%f, %f) Z=%f -> %f) \n ",nodeinfo[i],X[i],Y[i],B[i],S[i]);
463
}
464
}
465
/* for (i=0; i<nodesinpartition;++i){ */
466
/* fscanf(infids[1],"%i %i %le %le %le",&nodeinfo[i], &dummyint, &X[i], &Y[i], &B[i]); */
467
/* if (interpolateDEM==1){ */
468
/* printf("(%s) DEM interpolation yet not implemented\n",argv[0]); */
469
/* return EXIT_FAILURE; */
470
/* }else{ */
471
/* S[i] = B[i] + depth; */
472
/* printf("X(%i)= (%f, %f) Z=%f -> %f) \n ",nodeinfo[i],X[i],Y[i],B[i],S[i]); */
473
/* } */
474
/* } */
475
476
477
478
// load original mesh (footprint) elements
479
// elementinfo[8*k+0]/elementinfo[8*k+7] ... number/partition (if halo)
480
// elementinfo[8*k+1] ... body
481
// elementinfo[8*k+2] ... element type
482
// elementinfo[8*k+3..6] ... element nodes
483
// elementinfo[8*k+7] ... partition owing the element(see first line)
484
//---------------------------------------------------------------------
485
for (k=0,quads=0,triangles=0; k<elementsinpartition;++k){
486
fgets(instring, BUF_LEN-1,infids[2]);
487
488
//printf("%i: %s\n",k,instring);
489
sscanf(instring,"%s %i %i %i %i %i %i",&numberstring[0], &elementinfo[8*k+1],&elementinfo[8*k+2],
490
&elementinfo[8*k+3],&elementinfo[8*k+4],&elementinfo[8*k+5], &elementinfo[8*k+6]);
491
sscanf(numberstring,"%i%c%i",&elementinfo[8*k+0], &dummyc, &elementinfo[8*k+7]);
492
if (dummyc != '/') elementinfo[8*k+7] = 0;
493
if (elementinfo[8*k+2] == 404) quads++;
494
else if (elementinfo[8*k+2] == 303){
495
elementinfo[8*k+6] = -1;
496
triangles++;
497
} else {
498
printf("(%s): element type %i not recognised for entry %i in partition %i\n", argv[0], elementinfo[8*k+2],k,partition);
499
}
500
}
501
502
503
504
// load original mesh (footprint) boundary elements
505
// belementinfo[8*k+0]/elementinfo[8*k+7] ... num
506
// belementinfo[8*k+1] ... boundary
507
// belementinfo[8*k+2] ... parent 1
508
// belementinfo[8*k+3] ... parent 2
509
// belementinfo[8*k+4] ... element type
510
// belementinfo[8*k+5..6] ... element nodes
511
// belementinfo[8*k+7] ... partition owing the element(see first line)
512
//--------------------------------------------------------------------
513
for (k=0; k<belementsinpartition;++k){
514
/* fscanf(infids[3],"%s %i %i %i %i", &numberstring,&belementinfo[8*k+1],&belementinfo[8*k+2],&belementinfo[8*k+3],&belementinfo[8*k+4]); */
515
fgets(instring, BUF_LEN-1,infids[3]);
516
//printf("%i: %s\n",k,instring);
517
sscanf(instring,"%s %i %i %i %i %i %i",&numberstring[0], &belementinfo[8*k+1],&belementinfo[8*k+2],
518
&belementinfo[8*k+3],&belementinfo[8*k+4],&belementinfo[8*k+5], &belementinfo[8*k+6]);
519
sscanf(numberstring,"%i%c%i",&belementinfo[8*k+0], &dummyc, &belementinfo[8*k+7]);
520
//printf("%s: %i ,%c, %i\n",numberstring, belementinfo[8*k+0], dummyc, belementinfo[8*k+7]);
521
if (dummyc != '/') belementinfo[8*k+7] = 0;
522
if (belementinfo[8*k+4] == 202){
523
j=2;
524
}else if (belementinfo[8*k+4] == 101){
525
j=1;
526
belementinfo[8*k+6] = -1;
527
}else {
528
printf("(%s): boundary element type %i not recognised for entry %i in partition %i\n",
529
argv[0], belementinfo[8*k+4],k,partition);
530
}
531
/* printf("R %i/%i %i %i %i %i %i %i\n",belementinfo[8*k+0],belementinfo[8*k+7], */
532
/* belementinfo[8*k+1],belementinfo[8*k+2],belementinfo[8*k+3], */
533
/* belementinfo[8*k+4],belementinfo[8*k+5],belementinfo[8*k+6]); */
534
535
}
536
537
538
539
// read and write shared nodes file
540
//---------------------------------
541
k=0;
542
//printf("BUF_LEN = %i\n",BUF_LEN);
543
while(fgets(instring, BUF_LEN-1, infids[4]) != NULL) {
544
++k;
545
//printf("%i: %s\n", k, instring);
546
charptr = strtok(instring," ");
547
i=0;
548
while (charptr != NULL)
549
{
550
sharednodeinfo[i++] = atoi(charptr);
551
//printf("%s->%i ", charptr,sharednodeinfo[i-1]);
552
charptr = strtok(NULL," ");
553
}
554
//printf("\n");
555
for(level=0;level<levels;++level){
556
fprintf(outfids[4],"%i %i", pointsinlevel*level + sharednodeinfo[0],sharednodeinfo[1]);
557
for (j=2;j<i;++j){
558
fprintf(outfids[4]," %i", sharednodeinfo[j]);
559
}
560
fprintf(outfids[4],"\n");
561
}
562
}
563
564
// write extruded mesh
565
//--------------------
566
for (j=0,level=0;level<levels;++level){
567
for (k=0;k<nodesinpartition;++k){
568
l=100*k/pointsinlevel;
569
if (l>j){
570
j=l;
571
//printf("done: %3i percent\n", j);
572
}
573
dZ = (S[k] - B[k])/((double) levels - 1);
574
575
576
//printf("%i: S=%f, B=%f, dZ=%f\n",k,S[k],B[k],dZ);
577
578
fprintf(outfids[1],"%i -1 %e %e %e\n", pointsinlevel*level + nodeinfo[k], X[k], Y[k], B[k]+ ((double) level)*dZ ); //
579
580
}
581
}
582
583
// write elements
584
// elementinfo[8*k+0]/elementinfo[8*k+7] ... number/partition (if halo)
585
// elementinfo[8*k+1] ... body
586
// elementinfo[8*k+2] ... element type
587
// elementinfo[8*k+3..6] ... element nodes
588
// elementinfo[8*k+7] ... partition owing the element(see first line)
589
//--------------------------------------------------------------------
590
for (k=0;k<elementsinpartition;++k){
591
if (elementinfo[8*k+2] == 303) {elementtype = 706;l=3;}
592
else if (elementinfo[8*k+2] == 404) {elementtype = 808;l=4;}
593
for (level=0;level<levels-1;++level){
594
fprintf(outfids[2],"%i", elementsinlevel*level + elementinfo[8*k+0]); // number
595
if (elementinfo[8*k+7] > 0)
596
fprintf(outfids[2],"/%i ", elementinfo[8*k+7]); // if halo, then owner partition
597
fprintf(outfids[2]," %i %i", elementinfo[8*k+1], elementtype); // body, elementinfo[8*k+ 2]
598
for (j=0;j<l;++j) fprintf(outfids[2]," %i", pointsinlevel*level + elementinfo[8*k+ 3 + j]); //lower level points
599
for (j=0;j<l;++j) fprintf(outfids[2]," %i", pointsinlevel*(level + 1) + elementinfo[8*k+ 3 + j]);//upper level points
600
fprintf(outfids[2],"\n");
601
}
602
}
603
604
// write boundary elements
605
// belementinfo[8*k+0]/belementinfo[8*k+7] ... num
606
// belementinfo[8*k+1] ... boundary (off-setted by 2 for original ones)
607
// belementinfo[8*k+2] ... parent 1
608
// belementinfo[8*k+3] ... parent 2
609
// belementinfo[8*k+4] ... element type
610
// belementinfo[8*k+5..6] ... element nodes
611
// belementinfo[8*k+7] ... partition owing the element(see first line)
612
//--------------------------------------------------------------------
613
// bottom and surface (new, bcids=1,2)
614
parent[1]=0;
615
for (i=0,lines=0;i<2;++i){ // i==0 ... bottom, i==1 ... top
616
for (k=0;k<elementsinpartition;++k){
617
parent[0] = elementsinlevel*(levels-2)*i + elementinfo[8*k+0];
618
fprintf(outfids[3],"%i", i*elementsinpartition + k +1); // element number
619
if (elementinfo[8*k+7] > 0)
620
fprintf(outfids[3],"/%i", elementinfo[8*k+7]); // if halo, then owner partition
621
fprintf(outfids[3]," %1i %i %i %3i", i+1, parent[0], parent[1], elementinfo[8*k+2]); // bcid, parents, type
622
if (elementinfo[8*k+2] == 303) {l=3;}
623
else if (elementinfo[8*k+2] == 404) {l=4;}
624
for(j=0;j<l;++j){
625
fprintf(outfids[3]," %i", pointsinlevel*(levels-1)*i + elementinfo[8*k+ 3 + j]);
626
}
627
fprintf(outfids[3],"\n");
628
}
629
}
630
631
// already existing boundaries (extruded sides, bcids = original + 2)
632
for (k=0;k<belementsinpartition;++k){ //belementsinpartition
633
for (level=0;level<levels-1;++level){
634
for (i=0;i<2;++i)
635
parent[i] = (belementinfo[8*k+2+i] > 0) ? (level*elementsinlevel + belementinfo[8*k+2+i]) : 0;
636
if (belementinfo[8*k+4] == 202){
637
belementtype = 404;
638
++quads;
639
j=2;
640
}else if(belementinfo[8*k+4] == 101){
641
belementtype = 202;
642
++lines;
643
j=1;
644
}
645
else{
646
printf("(%s) boundary element type %3i of input file %s (line %i) not defined\n",argv[0],belementinfo[8*k+4],inputfile[3],k);
647
return EXIT_FAILURE;
648
}
649
/* printf("W %i/%i %i %i %i %i %i %i\n",belementinfo[8*k+0],belementinfo[8*k+7], */
650
/* belementinfo[8*k+1]+2,belementinfo[8*k+2],belementinfo[8*k+3], */
651
/* belementinfo[8*k+4],belementinfo[8*k+5],belementinfo[8*k+6]); */
652
fprintf(outfids[3],"%i", 2*elementsinpartition + belementsinpartition*level + belementinfo[8*k+0]); // number
653
if (belementinfo[8*k+7] > 0)
654
fprintf(outfids[3],"/%i",belementinfo[8*k+7]); //partition (if halo)
655
fprintf(outfids[3]," %i %i %i %3i",
656
belementinfo[8*k+1]+2, parent[0], parent[1], belementtype); // bcid, parents, type
657
for (l=0;l<2;++l){
658
if (l==0) {
659
for (i=0;i<j;++i){
660
fprintf(outfids[3]," %i", pointsinlevel*level + belementinfo[8*k+5+i]);
661
//printf("low: %i + %i\n", pointsinlevel*(level+l) , belementinfo[8*k+5+i]);
662
}
663
}else{
664
for (i=j-1;i>-1;--i){
665
fprintf(outfids[3]," %i", pointsinlevel*(level+1) + belementinfo[8*k+5+i]);
666
//printf(" %i + %i\n", pointsinlevel*(level+l) , belementinfo[8*k+5+i]);
667
}
668
}
669
}
670
fprintf(outfids[3],"\n");
671
}
672
}
673
674
675
// close files
676
//------------
677
for (i=0;i<5;++i){
678
fclose(infids[i]);
679
fclose(outfids[i]);
680
}
681
// free memory
682
//------------
683
free(X);
684
free(Y);
685
free(S);
686
free(B);
687
free(nodeinfo);
688
free(elementinfo);
689
free(belementinfo);
690
free(sharednodeinfo);
691
692
free(infids);
693
for (i=0;i<5;++i)
694
free(inputfile[i]);
695
free(inputfile);
696
free(outfids);
697
for (i=0;i<5;++i)
698
free(outputfile[i]);
699
free(outputfile);
700
701
return EXIT_SUCCESS;
702
}
703
704
705
706
707
//---------------------------------------------------------------------------------------------------
708
// extrudes serial mesh
709
//---------------------------------------------------------------------------------------------------
710
int extrudeserial( char *argv[], char *inputfilename, char *outputfilename, int levels, double depth, int pointsinlevel, int elementsinlevel, int belementsinlevel, int interpolationScheme, double *bed, double *surf, double *thick, int *pointsInDEM, double cutOffValue, double wexp, int GL, double percentage, double ratio, int baseline, int corrbed, int *isnoval0, int *isnoval1, int *isnoval2 )
711
{//extrudeserial
712
int i,j,k,l,level, dummyint, nodesinpartition, elementsinpartition, belementsinpartition, points, quads, lines, triangles, bricks, wedges, parent[2], elementtype, belementtype, bcoffset, maxorigBCno;
713
char instring[BUF_LEN],dummys[BUF_LEN], numberstring[BUF_LEN], *charptr;
714
double dummydbl, dZ;
715
// arrays to be allocated
716
double *X,*Y,*S,*B, Z;
717
int *nodeinfo, *elementinfo, *belementinfo;
718
char **inputfile, **outputfile;
719
FILE **outfids, **infids, *infofid;
720
721
// allocate and initiate needed arrays for input
722
//----------------------------------------------
723
infids = (FILE **) malloc(5 * sizeof(FILE *));
724
inputfile = (char **) malloc(5 * sizeof(char *));
725
for (i=0;i<5;++i)
726
inputfile[i] = (char *) malloc((MAXPATHLEN+1) * sizeof(char));
727
728
for (i=0;i<5;++i){
729
strcpy(inputfile[i],inputfilename);
730
}
731
strcat(inputfile[0],"/mesh.header");//header
732
strcat(inputfile[1],"/mesh.nodes");//elements
733
strcat(inputfile[2],"/mesh.elements");//elements
734
strcat(inputfile[3],"/mesh.boundary");//boundary
735
for (i=0;i<5;++i){
736
infids[i] = fopen(inputfile[i], "r");
737
if (infids[i] != NULL) continue;
738
printf("(%s) Could not open file %s\n",argv[0],inputfile[i]);
739
return EXIT_FAILURE;
740
}
741
// allocate and initiate needed arrays for output
742
//-----------------------------------------------
743
outfids = (FILE **) malloc(4 * sizeof(FILE *));
744
outputfile = (char **) malloc(4 * sizeof(char *));
745
for (i=0;i<4;++i)
746
outputfile[i] = (char *) malloc((MAXPATHLEN+1) * sizeof(char));
747
748
for (i=0;i<4;++i){
749
strcpy(outputfile[i],outputfilename);
750
}
751
strcat(outputfile[0],"/mesh.header");//header
752
strcat(outputfile[1],"/mesh.nodes");//elements
753
strcat(outputfile[2],"/mesh.elements");//elements
754
strcat(outputfile[3],"/mesh.boundary");//boundary
755
for (i=0;i<4;++i){
756
outfids[i] = fopen(outputfile[i], "w");
757
if (outfids[i] != NULL) continue;
758
printf("(%s) Could not open file %s\n",argv[0],outputfile[i]);
759
return EXIT_FAILURE;
760
}
761
for (i=0;i<4;++i){
762
printf("<-%s\n",inputfile[i]);
763
printf("->%s\n",outputfile[i]);
764
}
765
// read in header in order to
766
// inquire mesh sizes and open input files
767
//-----------------------------------------
768
fscanf(infids[0],"%i ",&nodesinpartition);
769
fscanf(infids[0],"%i ",&elementsinpartition);
770
fscanf(infids[0],"%i ",&belementsinpartition);
771
772
fscanf(infids[0],"%i ",&j);
773
points = lines = triangles = quads = 0;
774
for (i=0;i<j;++i){
775
fscanf(infids[0],"%i %i", &elementtype, &dummyint);
776
if (elementtype == 101) points = dummyint;
777
else if (elementtype == 202) lines = dummyint;
778
else if (elementtype == 303) triangles = dummyint;
779
else if (elementtype == 404) quads = dummyint;
780
else{
781
printf("(%s) element type %3i of input file %s not defined\n",argv[0],elementtype,inputfile[0]);
782
return EXIT_FAILURE;
783
}
784
}
785
786
printf("(%s) Serial mesh input: %i nodes, %i elements, %i boundary elements\n",argv[0], nodesinpartition,elementsinpartition,belementsinpartition);
787
// now we also know the amount of wedges and bricks
788
wedges = triangles*(levels - 1); // extruded triangles
789
bricks = quads*(levels -1); // extruded quads
790
triangles *= 2; // double, as free surface and bottom
791
quads *= 2; // double, as free surface and bottom
792
quads += (levels -1)*lines; // plus the extruded lines
793
lines = (levels - 1)*points + baseline*lines; // all extruded points and included baselines
794
795
// write partition header
796
//printf("%i %i + %i %i\n",points, wedges, bricks, quads+triangles+lines);
797
fprintf(outfids[0],"%i %i %i\n",nodesinpartition*levels, wedges+bricks, quads+triangles+lines); // no. points, no- elements, no. boundary elements
798
fprintf(outfids[0],"%i\n",(quads > 0) + (triangles > 0) + (bricks > 0) + (wedges > 0) + (lines > 0)); // no of different element types
799
if (lines > 0) fprintf(outfids[0],"202 %i\n", lines);
800
if (triangles > 0) fprintf(outfids[0],"303 %i\n", triangles);
801
if (quads > 0) fprintf(outfids[0],"404 %i\n", quads);
802
if (wedges > 0) fprintf(outfids[0],"706 %i\n", wedges);
803
if (bricks > 0) fprintf(outfids[0],"808 %i\n", bricks);
804
printf("(%s) Serial mesh output: %i nodes, %i elements, %i boundary elements\n\n",argv[0], nodesinpartition*levels, wedges+bricks, quads+triangles+lines);
805
806
807
X = (double *) malloc(nodesinpartition * sizeof(double));
808
Y = (double *) malloc(nodesinpartition * sizeof(double));
809
S = (double *) malloc(nodesinpartition * sizeof(double));
810
B = (double *) malloc(nodesinpartition * sizeof(double));
811
nodeinfo = (int *) malloc(nodesinpartition * sizeof(int));
812
elementinfo = (int *) malloc(elementsinpartition * 7 * sizeof(int));
813
belementinfo = (int *) malloc(belementsinpartition * 7 * sizeof(int));
814
815
816
// load original mesh (footprint) nodes
817
// and compute lower as well as upper
818
// Z-coordinate
819
//-------------------------------------
820
for (i=0; i<nodesinpartition;++i){
821
fscanf(infids[1],"%i %i %le %le %le",&nodeinfo[i], &dummyint, &X[i], &Y[i], &B[i]);
822
if (interpolationScheme > 0){
823
if (interpolatePoint(X[i], Y[i], &B[i], &S[i], bed, surf, thick, pointsInDEM, cutOffValue, wexp, interpolationScheme, argv, isnoval0, isnoval1, isnoval2) == EXIT_FAILURE){
824
fprintf(stderr,"(%s) Failed interpolating values for point %i %e %e\n",argv[0],i,X[i],Y[i]);
825
return EXIT_FAILURE;
826
}else{
827
//printf("S,B= %e %e\n", B[i], S[i]);
828
if (S[i] - B[i] < depth){
829
printf("corrected S[%i] %e %e %e\n", i, X[i], Y[i], S[i]);
830
if (corrbed == 1){
831
B[i] = S[i] - depth;
832
}else{
833
S[i] = B[i] + depth;
834
}
835
printf(" -> %e %e %e\n", X[i], Y[i], S[i]);
836
}
837
//S[i] = (S[i] <= B[i] - depth) ? (B[i] + depth) : S[i];
838
}
839
}else{ // constant extrusion offset
840
S[i] = B[i] + depth;
841
//printf("X(%i)= (%f, %f) Z=%f -> %f) \n ",nodeinfo[i],X[i],Y[i],B[i],S[i]);
842
}
843
}
844
845
846
// load original mesh (footprint) elements
847
// elementinfo[7*k+0] ... number
848
// elementinfo[7*k+1] ... body
849
// elementinfo[7*k+2] ... element type
850
// elementinfo[7*k+3..6] ... element nodes
851
//---------------------------------------------------------------------
852
for (k=0,quads=0,triangles=0; k<elementsinpartition;++k){
853
fgets(instring, BUF_LEN-1,infids[2]);
854
855
//printf("%i: %s\n",k,instring);
856
sscanf(instring,"%i %i %i %i %i %i %i",&elementinfo[7*k+0], &elementinfo[7*k+1],&elementinfo[7*k+2],
857
&elementinfo[7*k+3],&elementinfo[7*k+4],&elementinfo[7*k+5], &elementinfo[7*k+6]);
858
if (elementinfo[7*k+2] == 404) quads++;
859
else if (elementinfo[7*k+2] == 303){
860
elementinfo[7*k+6] = -1;
861
triangles++;
862
} else {
863
printf("(%s): element type %i not recognised for entry %i\n", argv[0], elementinfo[7*k+2],k);
864
}
865
}
866
867
868
869
// load original mesh (footprint) boundary elements
870
// belementinfo[7*k+0] ... number
871
// belementinfo[7*k+1] ... boundary
872
// belementinfo[7*k+2] ... parent 1
873
// belementinfo[7*k+3] ... parent 2
874
// belementinfo[7*k+4] ... element type
875
// belementinfo[7*k+5..6] ... element nodes
876
//--------------------------------------------------------------------
877
maxorigBCno = 0;
878
for (k=0; k<belementsinpartition;++k){
879
/* fscanf(infids[3],"%s %i %i %i %i", &numberstring,&belementinfo[7*k+1],&belementinfo[7*k+2],&belementinfo[7*k+3],&belementinfo[7*k+4]); */
880
fgets(instring, BUF_LEN-1,infids[3]);
881
//printf("%i: %s\n",k,instring);
882
sscanf(instring,"%i %i %i %i %i %i %i",&belementinfo[7*k+0], &belementinfo[7*k+1],&belementinfo[7*k+2],
883
&belementinfo[7*k+3],&belementinfo[7*k+4],&belementinfo[7*k+5], &belementinfo[7*k+6]);
884
if (belementinfo[7*k+4] == 202){
885
j=2;
886
}else if (belementinfo[7*k+4] == 101){
887
j=1;
888
belementinfo[7*k+6] = -1;
889
}else {
890
printf("(%s): boundary element type %i not recognised for entry %i\n",
891
argv[0], belementinfo[7*k+4],k);
892
}
893
/* printf("R %i/%i %i %i %i %i %i %i\n",belementinfo[7*k+0],belementinfo[7*k+7], */
894
/* belementinfo[7*k+1],belementinfo[7*k+2],belementinfo[7*k+3], */
895
/* belementinfo[7*k+4],belementinfo[7*k+5],belementinfo[7*k+6]); */
896
maxorigBCno = (belementinfo[7*k+1] > maxorigBCno) ? belementinfo[7*k+1]:maxorigBCno;
897
}
898
899
900
// write extruded mesh
901
//--------------------
902
for (j=0,level=0;level<levels;++level){
903
for (k=0;k<nodesinpartition;++k){
904
l=100*k/pointsinlevel;
905
//if (l>j){
906
//j=l;
907
//printf("done: %3i percent\n", j);
908
//}
909
// dZ = (S[k] - B[k])/((double) levels - 1);
910
Z = refinedHeightBL(S[k], B[k], levels, level, GL, percentage, ratio);
911
//if (percentage < 0) Z=S[k] + Z + B[k];
912
913
//printf("%i: S=%f, B=%f, dZ=%f\n",k,S[k],B[k],dZ);
914
915
fprintf(outfids[1],"%i -1 %e %e %e\n", pointsinlevel*level + nodeinfo[k], X[k], Y[k], Z ); //
916
917
// fprintf(outfids[1],"%i -1 %e %e %e\n", pointsinlevel*level + nodeinfo[k], X[k], Y[k], B[k]+ ((double) level)*dZ ); //
918
}
919
}
920
921
// write elements
922
// elementinfo[7*k+0] ... number
923
// elementinfo[7*k+1] ... body
924
// elementinfo[7*k+2] ... element type
925
// elementinfo[7*k+3..6] ... element nodes
926
//--------------------------------------------------------------------
927
for (k=0,bcoffset=1;k<elementsinpartition;++k){
928
if (elementinfo[7*k+2] == 303) {elementtype = 706;l=3;}
929
else if (elementinfo[7*k+2] == 404) {elementtype = 808;l=4;}
930
for (level=0;level<levels-1;++level){
931
fprintf(outfids[2],"%i", elementsinlevel*level + elementinfo[7*k+0]); // number
932
fprintf(outfids[2]," %i %i", elementinfo[7*k+1], elementtype); // body, elementinfo[7*k+ 2]
933
bcoffset = (bcoffset > elementinfo[7*k+1]) ? bcoffset : elementinfo[7*k+1];
934
for (j=0;j<l;++j) fprintf(outfids[2]," %i", pointsinlevel*level + elementinfo[7*k+ 3 + j]); //lower level points
935
for (j=0;j<l;++j) fprintf(outfids[2]," %i", pointsinlevel*(level + 1) + elementinfo[7*k+ 3 + j]);//upper level points
936
fprintf(outfids[2],"\n");
937
}
938
}
939
940
printf("bodies: %i\n", bcoffset);
941
942
// write boundary elements
943
// belementinfo[7*k+0] ... number
944
// belementinfo[7*k+1] ... boundary (off-setted by 2 for original ones)
945
// belementinfo[7*k+2] ... parent 1
946
// belementinfo[7*k+3] ... parent 2
947
// belementinfo[7*k+4] ... element type
948
// belementinfo[7*k+5..6] ... element nodes
949
//--------------------------------------------------------------------
950
// bottom and surface (new, bcids=1..bcoffset,bcoffset+1..2*bcoffset)
951
parent[1]=0;
952
for (i=0,lines=0;i<2;++i){ // i==0 ... bottom, i==1 ... top
953
for (k=0;k<elementsinpartition;++k){
954
parent[0] = elementsinlevel*(levels-2)*i + elementinfo[7*k+0];
955
fprintf(outfids[3],"%i", i*elementsinpartition + k +1); // element number
956
fprintf(outfids[3]," %i %i %i %3i", elementinfo[7*k+1]+i*bcoffset, parent[0], parent[1], elementinfo[7*k+2]); // bcid, parents, type
957
if (elementinfo[7*k+2] == 303) {l=3;}
958
else if (elementinfo[7*k+2] == 404) {l=4;}
959
for(j=0;j<l;++j){
960
fprintf(outfids[3]," %i", pointsinlevel*(levels-1)*i + elementinfo[7*k+ 3 + j]);
961
}
962
fprintf(outfids[3],"\n");
963
}
964
}
965
966
// already existing boundaries (extruded sides, bcids = original + 2)
967
for (k=0;k<belementsinpartition;++k){ //belementsinpartition
968
for (level=0;level<levels-1;++level){
969
for (i=0;i<2;++i)
970
parent[i] = (belementinfo[7*k+2+i] > 0) ? (level*elementsinlevel + belementinfo[7*k+2+i]) : 0;
971
if (belementinfo[7*k+4] == 202){
972
belementtype = 404;
973
++quads;
974
j=2;
975
}else if(belementinfo[7*k+4] == 101){
976
belementtype = 202;
977
++lines;
978
j=1;
979
}
980
else{
981
printf("(%s) boundary element type %3i of input file %s (line %i) not defined\n",argv[0],belementinfo[7*k+4],inputfile[3],k);
982
return EXIT_FAILURE;
983
}
984
fprintf(outfids[3],"%i", 2*elementsinpartition + belementsinpartition*level + belementinfo[7*k+0]); // number
985
fprintf(outfids[3]," %i %i %i %3i",
986
belementinfo[7*k+1]+2*bcoffset, parent[0], parent[1], belementtype); // bcid, parents, type
987
for (l=0;l<2;++l){
988
if (l==0) {
989
for (i=0;i<j;++i){
990
fprintf(outfids[3]," %i", pointsinlevel*level + belementinfo[7*k+5+i]);
991
//printf("low: %i + %i\n", pointsinlevel*(level+l) , belementinfo[7*k+5+i]);
992
}
993
}else{
994
for (i=j-1;i>-1;--i){
995
fprintf(outfids[3]," %i", pointsinlevel*(level+1) + belementinfo[7*k+5+i]);
996
//printf(" %i + %i\n", pointsinlevel*(level+l) , belementinfo[7*k+5+i]);
997
}
998
}
999
}
1000
fprintf(outfids[3],"\n");
1001
}
1002
}
1003
1004
// include the frame of the 2d footprint (e.g., in order to place BC for DIM-1 problem)
1005
if (baseline) {
1006
for (k=0;k<belementsinpartition;++k){ //belementsinpartition
1007
parent[0] = parent[1] = 0;
1008
// for (i=0;i<2;++i)
1009
// parent[i] = (belementinfo[7*k+2+i] > 0) ? (belementinfo[7*k+2+i]) : 0;
1010
belementtype = belementinfo[7*k+4];
1011
if (belementtype == 202) {++lines; j=2;}
1012
else if (belementtype == 101) {++points; j=2;}
1013
fprintf(outfids[3],"%i", 2*elementsinpartition + belementsinpartition*(levels-1) + belementinfo[7*k+0]); //number
1014
fprintf(outfids[3]," %i %i %i %3i",
1015
belementinfo[7*k+1] + (2*bcoffset) + maxorigBCno, parent[0], parent[1], belementtype); // bcid, parents, type
1016
for (i=0;i<j;++i){
1017
fprintf(outfids[3]," %i", belementinfo[7*k+5+i]);
1018
}
1019
fprintf(outfids[3],"\n");
1020
}
1021
}
1022
1023
1024
// close files
1025
//------------
1026
for (i=0;i<4;++i){
1027
fclose(infids[i]);
1028
fclose(outfids[i]);
1029
}
1030
// free memory
1031
//------------
1032
free(X);
1033
free(Y);
1034
free(S);
1035
free(B);
1036
free(nodeinfo);
1037
free(elementinfo);
1038
free(belementinfo);
1039
1040
free(infids);
1041
for (i=0;i<4;++i)
1042
free(inputfile[i]);
1043
free(inputfile);
1044
free(outfids);
1045
for (i=0;i<4;++i)
1046
free(outputfile[i]);
1047
free(outputfile);
1048
1049
return EXIT_SUCCESS;
1050
}
1051
1052
1053
1054
int main(int argc, char *argv[])
1055
{
1056
int i,j,k,l,levels,partitions,partition,pointsinlevel, elementsinlevel, belementsinlevel, stat, interpolateDEM, fileExists[3], interpolationScheme, noelementtypes,triangles,quads,lines,points, pointsInDEM[3],dummyint,GL, baseline, *isnoval0, *isnoval1, *isnoval2, corrbed=0;
1057
char inputdirectoryname[MAXPATHLEN+1],outputdirectoryname[MAXPATHLEN+1],directoryname[MAXPATHLEN+1], cpartition[11], inputfilename[MAXPATHLEN+1], outputfilename[MAXPATHLEN+1], filename[MAXPATHLEN+1], *charptr;
1058
double depth, cutOffValue, *bed, *surf, *thick, wexp, noval, percentage, ratio;
1059
FILE *infofid, *headerfid;
1060
1061
// failure/usage message
1062
//----------------------
1063
if((argc != 10) && ((argc != 14)) && (argc != 15)){
1064
printf("%s usage:\a\n\n",argv[0]);
1065
printf("%s inputdir outputdir levels extrudedepth N baseline GL percentage ratio DEM cutoff wexp noval\n\n",argv[0]);
1066
printf(" inputdir ... directory containing the 2D footrpint to be extruded\n");
1067
printf(" outputdir ... directory (will be created if not existing) containing\n");
1068
printf(" the extruded mesh\n");
1069
printf(" WARNING: Files in outputdir will be overwritten!\n");
1070
printf(" levels ... levels in direction of extrushion (>2)\n");
1071
printf(" extrudedepth ... depth in direction of extrushion in unit-length of\n");
1072
printf(" input mesh\n");
1073
printf(" N ... partitions\n");
1074
printf(" inputdir should contain an already partitioned mesh)\n");
1075
printf(" give N=1 for serial mesh\n");
1076
printf(" baseline ... 1/0\n");
1077
printf(" 1 if the existing side boundary for footprint (point/line in 2d/3d)\n");
1078
printf(" shall be included, else excluded\n\n");
1079
printf(" GL ... number of layers inside boundary layer\n");
1080
printf(" has to be smaller than levels\n");
1081
printf(" give 0, if no boundary layer wanted\n");
1082
printf(" percentage ... percentage height of boundary layer\n");
1083
printf(" with respect to the local height (as 0..1) \n");
1084
printf(" if positive BL at bottom, if negative at the surface \n");
1085
printf(" ratio ... ratio between two adjacent layers in boundary layer(>1) \n");
1086
printf(" has to be >1, if it's 0 automatic calculation, but does not work always \n");
1087
1088
printf(" DEM ... (optional) directory of digital elevation model\n");
1089
printf(" for interpolation of bed/surface (see below)\n");
1090
printf(" cutoff ... (optional, but mandatory if previous was given)\n");
1091
printf(" cutoff radius for interpolation\n");
1092
printf(" wexp ... (optional, but mandatory if previous was given)\n");
1093
printf(" exponent of weight for interpolation w=1/r^wexp\n");
1094
printf(" noval ... (optional, but mandatory if previous was given)\n");
1095
printf(" value indicating void/invalid entry in DEM (e.g. -999.99)\n");
1096
printf(" corrbed ... (optional) value: 1 or any other number\n");
1097
printf(" corrections induced by minimum flowdpeth are by default applied\n");
1098
printf(" correctiong the side of the free surface, only of value 1 is defined\n");
1099
printf(" the surface is kept constant and the bedrock is adjusted\n");
1100
printf("outputdir contains:\n");
1101
printf(" mesh.{nodes,elements,boundary} ... mesh files (if N==1)\n");
1102
printf(" partitioning.N/mesh.{nodes,elements,boundary} ... mesh files (if N>1)\n");
1103
printf(" info.dat ... file containing information on the extruded mesh\n");
1104
printf("DEM (if chosen) should provide (two of these three):\n");
1105
printf(" DEM/bed.xyz (hard-coded name!)\n");
1106
printf(" DEM/surf.xyz (hard-coded name!)\n");
1107
printf(" DEM/thick.xyz (hard-coded name!)\n");
1108
printf(" mind, that those filenames are hardcoded!\n");
1109
printf(" the xyz-files can contain a set of irregullary distributed (x,y,z)\n");
1110
printf(" coordinates, between those the mesh will be interpolated.\n");
1111
printf(" In case of a DEM being provided, the earlier given\n");
1112
printf(" parameter \"extrudedepth\" is taken as the minimum flow depth\n\n");
1113
printf(" Boundary layer implemented only in serial\n\n");
1114
return EXIT_FAILURE;
1115
}
1116
1117
// inquire parameters
1118
//-------------------
1119
levels = atoi(argv[3]);
1120
depth = atof(argv[4]);
1121
partitions = atoi(argv[5]);
1122
baseline = atoi(argv[6]);
1123
if (baseline != 1) baseline = 0;
1124
GL = atoi(argv[7]);
1125
if (GL >= levels) {
1126
fprintf(stderr, "GL = %i >= levels = %i !\n", GL, levels);
1127
return EXIT_FAILURE;
1128
}
1129
percentage = atof(argv[8]);
1130
if ((percentage < -0.99) || (percentage > 0.99)) {
1131
fprintf(stderr, "0.01 < percentage = %f10.4 < 0.99 !\n", percentage);
1132
return EXIT_FAILURE;
1133
}
1134
ratio = atof(argv[9]);
1135
if (ratio < 1.0 && ratio != 0) {
1136
fprintf(stderr, "ratio < 1.0 = %f10.4 < 1.0 or equal 0!\n", ratio);
1137
return EXIT_FAILURE;
1138
}
1139
1140
printf("(%s) executing with following inputs ...\n",argv[0]);
1141
printf(" ... input directory: %s \n",argv[1]);
1142
printf(" ... output directory: %s\n",argv[2]);
1143
printf(" ... levels: %i\n",levels);
1144
printf(" ... depth: %10.4f\n",depth);
1145
printf(" ... partitions: %i\n",partitions);
1146
printf(" ... include baseline(1=yes,0=no): %i\n",baseline);
1147
printf(" ... GL: %i\n",GL);
1148
printf(" ... percentage: %10.4f\n",percentage);
1149
printf(" ... ratio: %10.4f\n",ratio);
1150
if (partitions < 1){
1151
printf("(%s) Wrong number of partitions %i\n",argv[0],partitions);
1152
printf("(%s) Assuming serial mesh (N=1)\n",argv[0]);
1153
partitions=1;
1154
}
1155
interpolateDEM = 0;
1156
if ((argc == 14) || (argc == 15)){// we have a DEM to interpolate
1157
for (i=0;i<argc;++i) {
1158
printf("argv[%i] = %s\n",i, argv[i]);
1159
}
1160
1161
cutOffValue = atof(argv[11]);
1162
wexp = atof(argv[12]);
1163
noval = atof(argv[13]);
1164
printf(" ... DEM directory: %s\n", argv[10]);
1165
printf(" ... cutoff radius: %e\n", cutOffValue);
1166
printf(" ... exponent m of weight (1/r^m): %e\n", wexp);
1167
printf(" ... noval: %10.4f\n",noval);
1168
if (corrbed == 1){
1169
printf(" ... depth corrections of bedrock\n");
1170
}else{
1171
printf(" ... depth corrections of surface (default)\n");
1172
}
1173
1174
int entriesPerLine;
1175
1176
// the bedrock DEM
1177
printf("(%s) Checking for input files of DEM in directory %s\n", argv[0], argv[7]);
1178
strcpy(filename,argv[10]); strcat(filename,"/bed.xyz");
1179
if (file_exists(filename)){
1180
pointsInDEM[0] = checkFileEntries(filename, argv, &entriesPerLine);
1181
if ((pointsInDEM[0] < 1) || (entriesPerLine < 3)){
1182
fprintf(stderr,"(%s) Wrong file entries (%i,%i) found in file %s\n", argv[0], pointsInDEM[0], entriesPerLine, filename);
1183
return EXIT_FAILURE;
1184
}else{
1185
printf("(%s) %i file entries found in file %s\n", argv[0], pointsInDEM[0], filename);
1186
bed = (double *) malloc(pointsInDEM[0] * 3 * sizeof(double));
1187
// for (i=0;i<pointsInDEM[0];++i){ bed[3*i]=(double) 3*i, bed[(3*i)+1]=2.0, bed[(3*i)+2]=3.0; printf("-> %f %f %f\n",bed[3*i], bed[(3*i)+1], bed[(3*i)+2]);}
1188
isnoval0 = (int *) malloc( pointsInDEM[0] * sizeof(int));
1189
dummyint = readDEM(bed, isnoval0, pointsInDEM[0], filename, argv);
1190
if (dummyint > 1){
1191
printf("(%s) %i out of %i valid points in DEM %s\n",argv[0],dummyint,pointsInDEM[0],filename);
1192
pointsInDEM[0] = dummyint;
1193
// for (i=0;i<pointsInDEM[0];++i) printf("->%i / %i %f %f %f\n",i, pointsInDEM[0], bed[3*i], bed[(3*i)+1], bed[(3*i)+2]);
1194
} else {
1195
fprintf(stderr,"(%s) Error reading file %s\n",argv[0],filename);
1196
return EXIT_FAILURE;
1197
}
1198
interpolateDEM = 1;
1199
interpolationScheme = 1;
1200
}
1201
}else{
1202
interpolationScheme = 0;
1203
printf("(%s) no file %s\n", argv[0], filename);
1204
}
1205
1206
// the surface DEM
1207
strcpy(filename,argv[10]); strcat(filename,"/surf.xyz");
1208
if (file_exists(filename)){
1209
pointsInDEM[1] = checkFileEntries(filename, argv, &entriesPerLine);
1210
if ((pointsInDEM[1] < 1) || (entriesPerLine < 3)){
1211
fprintf(stderr,"(%s) Wrong file entries (%i,%i) found in file %s\n", argv[0], pointsInDEM[1], entriesPerLine, filename);
1212
return EXIT_FAILURE;
1213
}else{
1214
printf("(%s) %i file entries found in file %s\n", argv[0], pointsInDEM[1], filename);
1215
surf = (double *) malloc(pointsInDEM[1] * 3 * sizeof(double));
1216
for (i=0;i<pointsInDEM[1];++i){ surf[3*i]=0.0, surf[3*i+1]=0.0, surf[3*i+2]=0.0;}
1217
isnoval1 = (int *) malloc( pointsInDEM[1] * sizeof(int));
1218
dummyint = readDEM(surf, isnoval1, pointsInDEM[1], filename, argv);
1219
if (dummyint > 1){
1220
printf("(%s) %i out of %i valid points in DEM %s\n",argv[0],dummyint,pointsInDEM[1],filename);
1221
pointsInDEM[1] = dummyint;
1222
} else {
1223
fprintf(stderr,"(%s) Error reading file %s\n",argv[0],filename);
1224
return EXIT_FAILURE;
1225
}
1226
}
1227
interpolateDEM = 1;
1228
interpolationScheme += 10;
1229
}else{
1230
printf("(%s) no file %s\n", argv[0], filename);
1231
if (interpolationScheme == 0) {
1232
fprintf(stderr,"(%s) We either need a surface or a bedrock\n",argv[0]);
1233
return EXIT_FAILURE;
1234
}
1235
}
1236
1237
if (interpolationScheme < 11){
1238
// the thickness DEM
1239
strcpy(filename,argv[10]); strcat(filename,"/thick.xyz");
1240
if (file_exists(filename)){
1241
pointsInDEM[2] = checkFileEntries(filename, argv, &entriesPerLine);
1242
if ((pointsInDEM[2] < 1) || (entriesPerLine < 3)){
1243
fprintf(stderr,"(%s) Wrong file entries (%i,%i) found in file %s\n", argv[0], pointsInDEM[2], entriesPerLine, filename);
1244
return EXIT_FAILURE;
1245
}else{
1246
printf("(%s) %i file entries found in file %s\n", argv[0], pointsInDEM[2], filename);
1247
thick = (double *) malloc(pointsInDEM[2] * 3 * sizeof(double));
1248
for (i=0;i<pointsInDEM[2];++i){ thick[3*i]=0.0, thick[3*i+1]=0.0, thick[3*i+2]=0.0;}
1249
isnoval2 = (int *) malloc( pointsInDEM[0] * sizeof(int));
1250
dummyint = readDEM(thick, isnoval2, pointsInDEM[2], filename, argv);
1251
if (dummyint > 1){
1252
printf("(%s) %i out of %i valid points in DEM %s\n",argv[0],dummyint,pointsInDEM[2],filename);
1253
pointsInDEM[2] = dummyint;
1254
}else{
1255
fprintf(stderr,"(%s) Error reading file %s\n",argv[0],filename);
1256
return EXIT_FAILURE;
1257
}
1258
}
1259
interpolateDEM = 1;
1260
interpolationScheme += 100;
1261
}else{
1262
fprintf(stderr,"(%s) no file %s\n", argv[0], filename);
1263
fprintf(stderr,"(%s) We either need a thickness file\n",argv[0]);
1264
return EXIT_FAILURE;
1265
}
1266
}
1267
}else{
1268
interpolationScheme = 0;
1269
}
1270
1271
1272
1273
strcpy(inputdirectoryname,argv[1]);
1274
// read in global header file
1275
//---------------------------
1276
strcpy(filename,inputdirectoryname); strcat(filename,"/mesh.header");
1277
headerfid = fopen(filename, "r");
1278
if (headerfid==NULL){
1279
printf("(%s) Error opening file %s\n",argv[0],filename);
1280
return EXIT_FAILURE;
1281
}
1282
1283
printf("(%s) reading global header file %s\n",argv[0],filename);
1284
fscanf(headerfid,"%i %i %i",&pointsinlevel, &elementsinlevel,&belementsinlevel);
1285
fscanf(headerfid,"%i",&noelementtypes);
1286
points = lines = triangles = quads = 0;
1287
for (i=0;i<noelementtypes;++i){
1288
fscanf(headerfid,"%i %i",&l, &k);
1289
if (l == 101) points = k;
1290
else if (l == 202) lines = k;
1291
else if ( l == 303) triangles = k;
1292
else if (l == 404) quads = k;
1293
else {
1294
printf("(%s) element type %i in header file %s not recognised\n",argv[0],l,filename);
1295
return EXIT_FAILURE;
1296
}
1297
}
1298
//compose input and output directory name
1299
//---------------------------------------
1300
if (partitions > 1){// we have a parallel mesh
1301
if (partitions < 10)
1302
sprintf(cpartition,"%1i", partitions);
1303
else if (partitions < 100)
1304
sprintf(cpartition, "%2i", partitions);
1305
else if (partitions < 1000)
1306
sprintf(cpartition, "%3i", partitions);
1307
else if (partitions < 10000)
1308
sprintf(cpartition, "%4i", partitions);
1309
else{
1310
printf("(%s) More than 9999 partitions? Get real or (if you insist) change the source code of this executable!\n",argv[0]);
1311
return EXIT_FAILURE;
1312
}
1313
strcat(inputdirectoryname,"/partitioning.");strcat(inputdirectoryname,cpartition);
1314
printf("(%s) Reading from directory: %s\n",argv[0], inputdirectoryname);
1315
1316
strcpy(outputdirectoryname, argv[2]);
1317
printf("(%s) creating output file directory %s\n", argv[0], outputdirectoryname);
1318
MKDIR(outputdirectoryname);
1319
1320
strcat(outputdirectoryname,"/partitioning.");
1321
strcat(outputdirectoryname,cpartition);
1322
printf("(%s) creating output file partition directory %s\n", argv[0], outputdirectoryname);
1323
MKDIR(outputdirectoryname);
1324
1325
1326
}else{
1327
printf("(%s) Reading from directory: %s\n",argv[0], inputdirectoryname);
1328
1329
strcpy(outputdirectoryname, argv[2]);
1330
printf("(%s) creating output file directory %s\n", argv[0], outputdirectoryname);
1331
MKDIR(outputdirectoryname);
1332
}
1333
1334
strcpy(filename,outputdirectoryname); strcat(filename,"/info.dat");
1335
infofid = fopen(filename, "w");
1336
if (infofid==NULL){
1337
printf("(%s) Error opening file %s\n",argv[0],filename);
1338
return EXIT_FAILURE;
1339
}
1340
// write input to info file
1341
//-------------------------
1342
fprintf(infofid,"command line was: %s %s %s %i %8.2f %i %i %i %5.2f %4.2f %s %8.2f %3.2f %8.2f [%i]\n",argv[0], argv[1],argv[2],levels,depth, partitions,baseline,GL,percentage,ratio,argv[10],cutOffValue,wexp,noval,corrbed);
1343
fprintf(infofid,"(%s) was invoked with following inputs ...\n",argv[0]);
1344
fprintf(infofid," ... input directory: %s \n",argv[1]);
1345
fprintf(infofid," ... output directory: %s\n",argv[2]);
1346
fprintf(infofid," ... levels: %i\n",levels);
1347
fprintf(infofid," ... depth: %10.4f\n\n",depth);
1348
fprintf(infofid," ... partitions: %i\n",partitions);
1349
fprintf(infofid," ... baseline: %i\n",baseline);
1350
fprintf(infofid," ... GL: %i\n\n",GL);
1351
fprintf(infofid," ... percentage: %10.4f\n\n",percentage);
1352
fprintf(infofid," ... ratio: %10.4f\n\n",ratio);
1353
if (interpolateDEM){
1354
fprintf(infofid," ... DEM directory: %s\n",argv[10]);
1355
fprintf(infofid," ... cutoff radius: %10.4f\n", cutOffValue);
1356
fprintf(infofid," ... exponent m of weight (1/r^m): %10.4f\n",wexp);
1357
fprintf(infofid," ... noval: %10.4f\n\n",noval);
1358
fprintf(infofid," ... corrbed: %1i\n\n",corrbed);
1359
}
1360
1361
fprintf(infofid," ... total points in 2d mesh: %i \n",pointsinlevel);
1362
fprintf(infofid," ... total elements in 2d mesh: %i \n",elementsinlevel);
1363
fprintf(infofid," ... total boundary elements in 2d mesh: %i \n",belementsinlevel);
1364
fprintf(infofid," ... no of element type 101: %i \n",points);
1365
fprintf(infofid," ... no of element type 202: %i \n",lines);
1366
fprintf(infofid," ... no of element type 303: %i \n",triangles);
1367
fprintf(infofid," ... no of element type 404: %i \n",quads);
1368
printf(" ... total points in 2d mesh: %i \n",pointsinlevel);
1369
printf(" ... total elements in 2d mesh: %i \n",elementsinlevel);
1370
printf(" ... total boundary elements in 2d mesh: %i \n",belementsinlevel);
1371
printf(" ... no of element type 101: %9i \n",points);
1372
printf(" ... no of element type 202: %9i \n",lines);
1373
printf(" ... no of element type 303: %9i \n",triangles);
1374
printf(" ... no of element type 404: %9i \n",quads);
1375
fclose(headerfid);
1376
1377
// write global header file
1378
//-------------------------
1379
strcpy(filename,outputdirectoryname); strcat(filename,"/mesh.header.global");
1380
headerfid = fopen(filename, "w");
1381
if (headerfid==NULL){
1382
printf("(%s) Error opening file %s\n",argv[0],filename);
1383
return EXIT_FAILURE;
1384
}
1385
fprintf(headerfid,"%9i %9i %9i\n",pointsinlevel*levels,elementsinlevel*(levels-1),elementsinlevel*2 + belementsinlevel*(levels-1));
1386
// fprintf(headerfid,"%i\n",noelementtypes);
1387
fprintf(headerfid,"%9i\n", (points > 0) + 2*(triangles > 0) + (quads > 0) +1);
1388
if (points > 0) fprintf(headerfid,"202 %i\n",points * (levels-1)) + lines * baseline;
1389
if ((lines > 0) || (quads > 0)) fprintf(headerfid,"404 %i\n",lines * (levels-1) + 2 * quads);
1390
if (triangles > 0) fprintf(headerfid,"706 %i\n", triangles * (levels-1));
1391
if (quads > 0) fprintf(headerfid,"808 %i\n", quads * (levels-1));
1392
1393
1394
if (partitions > 1){
1395
// extrude meshes for each partition
1396
//----------------------------------
1397
for (partition=0;partition<partitions;++partition){
1398
if (partition < 10)
1399
sprintf(cpartition,"/part.%1i", partition+1);
1400
else if (partition < 100)
1401
sprintf(cpartition, "/part.%2i", partition+1);
1402
else if (partition < 1000)
1403
sprintf(cpartition, "/part.%3i", partition+1);
1404
else
1405
sprintf(cpartition, "/part.%4i", partition+1);
1406
strcpy(inputfilename,inputdirectoryname); strcat(inputfilename,cpartition);
1407
printf("(%s) reading partition %i: base file name %s\n",argv[0],partition+1,inputfilename);
1408
strcpy(outputfilename,outputdirectoryname); strcat(outputfilename,cpartition);
1409
stat = extrudepartition(argv, inputfilename, outputfilename, partition, partitions, levels, depth, pointsinlevel, elementsinlevel, belementsinlevel, interpolationScheme, bed, surf, thick, pointsInDEM, cutOffValue, wexp, isnoval0, isnoval1, isnoval2);
1410
//stat = extrudepartition(argv, inputfilename, outputfilename, partition, partitions, levels, depth, pointsinlevel, elementsinlevel, belementsinlevel, interpolateDEM=0, charptr);
1411
1412
} // end of loop over partitions
1413
}else{
1414
printf("(%s) Doing serial extrusion\n",argv[0]);
1415
strcpy(inputfilename,inputdirectoryname);
1416
strcpy(outputfilename,outputdirectoryname);
1417
stat = extrudeserial(argv, inputfilename, outputfilename, levels, depth, pointsinlevel, elementsinlevel, belementsinlevel, interpolationScheme, bed, surf, thick, pointsInDEM, cutOffValue, wexp, GL, percentage, ratio, baseline, corrbed, isnoval0, isnoval1, isnoval2);
1418
1419
}
1420
1421
1422
1423
fclose(headerfid);
1424
fclose(infofid);
1425
if (interpolationScheme > 0){
1426
free(bed);
1427
free(surf);
1428
}
1429
return stat;
1430
1431
}
1432
1433