dfnTrans
Code for Particle Tracking simulations in 3D DFN
ReadGridInit.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <search.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <string.h>
6 #include "FuncDef.h"
7 #include <unistd.h>
8 
9 struct inpfile {
10  char filename[120];
11  long int flag;
12  double param;
13 };
14 
16 void ReadInit()
19 {
20  /************** opening file params.txt ***************************************/
21  int i, nf;
22  char cs;
23  struct inpfile inputfile;
24  inputfile = Control_File("param:", 6);
25  printf("\n OPEN AND READ FILE: %s \n \n", inputfile.filename);
26  FILE *fpp = OpenFile (inputfile.filename, "r");
27 
28  if (fscanf(fpp, "%d\n", &nfract) != 1) {
29  printf("Error");
30  }
31 
32  printf(" Number of fractures in the domain = %d \n", nfract);
33  /********************** opening an inp file ***************************/
34  int nn, j;
35  inputfile = Control_File("inp:", 4);
36  printf("\n OPEN AND READ AVS FILE: %s \n \n", inputfile.filename);
37  FILE *fp = OpenFile (inputfile.filename, "r");
38 
39  if (fscanf(fp, "%d %d %d %d %d\n", &nnodes, &ncells, &nn, &nn, &nn ) != 5) {
40  printf("Error");
41  }
42 
43  printf(" Total number of nodes: %d, Total number of elements (triangles): %d\n", nnodes, ncells);
44  fclose(fp);
45  /******************* open and read stor file *******************************/
46  unsigned int nedges;
47  unsigned int snode_edge;
48  int area_coef;
49  inputfile = Control_File("stor:", 5 );
50  printf("\n OPEN AND READ STOR FILE: %s\n \n", inputfile.filename);
51  FILE *fps = OpenFile (inputfile.filename, "r");
52 
53  /* Read the head of the file */
54  for (i = 0; i < 2; i++) {
55  do {
56  cs = fgetc(fps);
57  } while (cs != '\n');
58  }
59 
60  int node1;
61 
62  if (fscanf (fps, " %d %d %d %d %d \n", &nedges, &node1, &snode_edge, &area_coef, &max_neighb ) != 5) {
63  printf("Error");
64  }
65 
66  printf (" Total number of edges in Voronoy polygons = %d, total number of nodes = %d \n", nedges, node1);
67 
68  if (node1 != nnodes) {
69  printf("the number of nodes in inp is not equal to number of nodes in stor file! \n");
70  }
71 
72  fclose(fps);
73  /***** after reading the number of nodes "nnodes"
74  the number of fractures "nfract"
75  the number of cells "ncells"
76  the memory is allocated for data structures ************************/
77  node = (struct vertex*) malloc (nnodes * sizeof(struct vertex));
78 
79  for (i = 0; i < nnodes; i++) {
80  node[i].indnodes = (unsigned int*) malloc(max_neighb * sizeof(unsigned int));
81  node[i].type = (unsigned int*) malloc(max_neighb * sizeof(unsigned int));
82  node[i].flux = (double*) malloc(max_neighb * sizeof(double));
83  node[i].area = (double*) malloc(max_neighb * sizeof(double));
84  node[i].cells = (unsigned int**) malloc (max_neighb * sizeof(unsigned int*));
85  node[i].fracts = (unsigned int**) malloc (max_neighb * sizeof(unsigned int*));
86 
87  for (j = 0; j < max_neighb; j++) {
88  node[i].cells[j] = (unsigned int*)malloc(4 * sizeof(unsigned int));
89  node[i].fracts[j] = (unsigned int*)malloc(4 * sizeof(unsigned int));
90  }
91  }
92 
93  if (node == NULL) {
94  printf("Allocation memory problem - node\n");
95  }
96 
97  /********** allocate memory for cell structure *****************************/
98  cell = (struct element*) malloc (ncells * sizeof(struct element));
99 
100  if (cell == NULL) {
101  printf("Allocation memory problem - cell\n");
102  }
103 
104  /********** allocate memory for fracture structure *****************************/
105  fracture = (struct material*)malloc (nfract * sizeof(struct material));
106 
107  if (fracture == NULL) {
108  printf("Allocation memory problem - fracture\n");
109  }
110 
111  printf("\n Memory allocation is done successfully \n");
112  /*****************reading the orientation angle and norm components
113  of every fracture from params.txt file*************************/
114  inputfile = Control_File_Optional("poly:", 5);
115 
116  if (inputfile.flag > 0) {
117  printf("\n OPEN AND READ FILE: %s \n \n", inputfile.filename);
118  FILE *fpo = OpenFile (inputfile.filename, "r");
119  int nnf = 0.0;
120 
121  for (i = 0; i < nfract; i++) {
122  if( fscanf(fpo, "%d %d %f %f %f %d %f %f %d\n", &nf, &nnf, &fracture[i].theta,
123  &fracture[i].nvect_xy[0], &fracture[i].nvect_xy[1], &nnf,
124  &fracture[i].nvect_z[0], &fracture[i].nvect_z[1], &nnf) != 9) {
125  printf("Error");
126  }
127  }
128 
129  fclose(fpo);
130  } else {
131  int nnf;
132 
133  for (i = 0; i < 7; i++) {
134  do {
135  cs = fgetc(fpp);
136  } while (cs != '\n');
137  }
138 
139  for (i = 0; i < nfract; i++) {
140  if (fscanf(fpp, "%d %f %f %f %d %f %f %d %d\n", &nf, &fracture[i].theta,
141  &fracture[i].nvect_xy[0], &fracture[i].nvect_xy[1], &nnf,
142  &fracture[i].nvect_z[0], &fracture[i].nvect_z[1], &nnf, &nnf) != 9) {
143  printf("Error");
144  }
145  }
146  }
147 
148  fclose(fpp);
149  return;
150 }
152 
153 
154 
158 {
159  struct inpfile inputfile;
160  unsigned int nedges;
161  unsigned int snode_edge;
162  unsigned int area_coef;
163  unsigned int n1, j, l, ln;
164  unsigned int nnv;
165  unsigned int ncv;
166  char c[3], line[20], cs;
167  unsigned long i;
168  /* Header of inp and stor files: */
169  /* nedges - total number of edges in Voronoy polygons in whole domain */
170  /* snode_edge - number of edges + number of nodes +1 */
171  /* area_coef - Can be (1,3, or 4) number of area coefficients. The area/distance coefficients are the area of a Voronoi face divided by the Delaunay edge length. This coefficient is either written as a scalar (1), a vector area (3), or both vector and scalar areas are written (4). */
172  /* max_neighb - maximum number of neighboring nodes */
173  /* nnv - number of nodes variables */
174  /* ncv - number of cells variables */
175  inputfile = Control_File("inp:", 4 );
176  FILE *fp = OpenFile (inputfile.filename, "r");
177  printf("\n OPEN AND READ FILE: %s \n ", inputfile.filename);
178 
179  if (fscanf(fp, "%d %d %d %d %d\n", &n1, &n1, &nnv, &ncv, &n1 ) != 5) {
180  printf("Error");
181  }
182 
183  /* read 3D coordinations: x in [0], y in [1], and z in [2] for every node i+1 */
184 
185  for (i = 0; i < nnodes; i++) {
186  if (fscanf(fp, "%d %lf %lf %lf \n", &n1, &node[i].coord[0], &node[i].coord[1], &node[i].coord[2]) != 4) {
187  printf("Error");
188  }
189  }
190 
191  // printf("\n Nodes 3D coordinations are read \n");
192  /* read a connectivity list: number of fracture and 3 node numbers for each cell (triangle) */
193  unsigned int current_fract = 1, previous_fract = 1;
194  unsigned int cell_f = 0;
195  fracture[0].firstcell = 1;
196 
197  for (i = 0; i < ncells; i++) {
198  if (fscanf(fp, "%d %d %c %c %c %d %d %d \n", &n1, &cell[i].fracture,
199  &c[0], &c[1], &c[2], &cell[i].node_ind[0], &cell[i].node_ind[1], &cell[i].node_ind[2]) != 8) {
200  printf("Error");
201  }
202 
203  current_fract = cell[i].fracture;
204 
205  if (current_fract != previous_fract) {
206  fracture[current_fract - 1].firstcell = i + 1;
207  previous_fract = current_fract;
208  fracture[current_fract - 2].numbcells = i + 1 - fracture[current_fract - 2].firstcell;
209  cell_f = i;
210  }
211  }
212 
213  fracture[nfract - 1].numbcells = ncells - cell_f;
214 
215  /* read the next lines, which show node attributes and thier order in next data block */
216 
217  if (fscanf(fp, "%d", &n1) != 1) {
218  printf("Error");
219  }
220 
221  int k = 1, imt1_ind = 0, itp1_ind = 0, ni[nnv];
222  char var_name[20];
223 
224  for (i = 0; i < nnv; i++) {
225  if (fscanf(fp, "%d", &ni[i]) != 1) {
226  printf("Error");
227  }
228 
229  k = k * ni[i];
230  }
231 
232  if (k != 1) {
233  printf(" Please check nodes attributes in inp file\n");
234  }
235 
236  /* read node's attribute's names and search for imt1 and itp1 */
237  for (i = 0; i < nnv; i++) {
238  if (fscanf(fp, "%s %s \n", var_name, line) != 2) {
239  printf("Error");
240  }
241 
242  int res = strncmp(var_name, "imt1,", 5);
243 
244  if (res == 0) {
245  imt1_ind = i;
246  }
247 
248  int res1 = strncmp(var_name, "itp1,", 5);
249 
250  if (res1 == 0) {
251  itp1_ind = i;
252  }
253  }
254 
255  /* read variables imt1 - node material (in our case it is fracture id) and itp1 - type of node */
256  /* type of node: 0 - interior; 10- exterior; 2- interior interface; 12- exterior interface */
257  double fn;
258  int fr = 0, currentfr = 1;
259  k = 0;
260  fracture[k].firstnode = 1;
261 
262  for (i = 0; i < nnodes; i++) {
263  node[i].fracture[0] = 0;
264  node[i].fracture[1] = 0;
265 
266  for (j = 0; j < itp1_ind + 2; j++) {
267  if (fscanf(fp, "%lf ", &fn) != 1) {
268  printf("Error");
269  }
270 
271  if (j == imt1_ind + 1) {
272  fr = (int)fn;
273 
274  if (currentfr != fr) {
275  fracture[k].lastnode = i;
276  k = k + 1;
277  fracture[k].firstnode = i + 1;
278  currentfr = fr;
279  }
280 
281  node[i].fracture[0] = (int)fn;
282  }
283 
284  if (j == itp1_ind + 1) {
285  node[i].typeN = (int)fn;
286  }
287  }
288 
289  do {
290  cs = fgetc(fp);
291  } while (cs != '\n');
292  }
293 
295  // printf(" \n Material types and type of nodes are read \n");
296  fclose(fp) ;
297  /******************* open and read stor file *******************************/
298  inputfile = Control_File("stor:", 5 );
299  FILE *fps = OpenFile (inputfile.filename, "r");
300  printf("\n OPEN AND READ FILE: %s \n \n", inputfile.filename);
301 
302  /* Read the head of the file */
303  for (i = 0; i < 2; i++) {
304  do {
305  cs = fgetc(fps);
306  } while (cs != '\n');
307  }
308 
309  unsigned int node1;
310 
311  if (fscanf (fps, " %d %d %d %d %d \n", &nedges, &node1, &snode_edge, &area_coef, &max_neighb ) != 5) {
312  printf("Error");
313  }
314 
315  if (node1 != nnodes) {
316  printf("the number of nodes in inp file is not equal to number of nodes in stor file! \n");
317  }
318 
319  /* Read volumes of Voronoy polygons. Each polygon and it's volume is associated
320  with one node (that is a center of polygon) *************************/
321 
322  for (i = 0; i < nnodes; i++) {
323  if (fscanf(fps, "%lf", &node[i].pvolume) != 1) {
324  printf("Error");
325  }
326  }
327 
328  // printf(" \n Volumes of Voronoi polygons are read \n");
329  /* Read an array with number of neighbors for each node */
330  unsigned int cn, pn;
331 
332  if (fscanf(fps, "%d", &pn) != 1) {
333  printf("Error");
334  }
335 
336  for (i = 0; i < nnodes; i++) {
337  if (fscanf(fps, "%d", &cn) != 1) {
338  printf("Error");
339  }
340 
341  node[i].numneighb = cn - pn;
342  pn = cn;
343  }
344 
345  /* Read indexes of neighboring nodes (including its node number)*/
346 
347  for (i = 0; i < nnodes; i++) {
348  for (j = 0; j < node[i].numneighb; j++) {
349  if (fscanf(fps, "%d", &node[i].indnodes[j]) != 1) {
350  printf("Error");
351  }
352  }
353  }
354 
355  // printf(" \n Number of neighbors of each node and their indices are read \n ");
356 
357  /***Read pointers to area cofficients, zeros and diagonal elements in stor file **/
358 
359  for (i = 0; i < nedges; i++) {
360  if (fscanf(fps, "%d", &cn) != 1) {
361  printf("Error");
362  }
363  }
364 
365  /*read zeros*/
366  for (i = 0; i < nnodes + 1; i++) {
367  if (fscanf(fps, "%d", &cn) != 1) {
368  printf("Error");
369  }
370  }
371 
372  /* read diagonal elements*/
373  int count = 0, count1 = 0;
374 
375  if (fscanf(fps, "%d", &pn) != 1) {
376  printf("Error");
377  }
378 
379  for (i = 0; i < nnodes - 1; i++) {
380  if (fscanf(fps, "%d", &cn) != 1) {
381  printf("Error");
382  }
383 
384  count = count + cn - pn;
385  count1 = count1 + node[i].numneighb;
386  pn = cn;
387  }
388 
389  /**** reading area coefficients *************************/
390 
391  for (i = 0; i < nnodes; i++) {
392  node[i].aperture = 0.0;
393 
394  for (j = 0; j < max_neighb; j++) {
395  if (j < node[i].numneighb) {
396  if (fscanf(fps, "%lf", &node[i].area[j]) != 1) {
397  printf("Error");
398  }
399  }
400  }
401  }
402 
403  fclose(fps);
404  /****** reading aperture file **************************/
407  inputfile = Control_File("aperture:", 9);
408  int res;
409  res = strncmp(inputfile.filename, "yes", 3);
410 
411  if (res == 0) {
412  ReadAperture();
413  } else {
414  printf("\n There is no aperture file is defined. All fractures will have constant aperture equal to 'thickness' parameter. \n");
415 
416  for (i = 0; i < nnodes; i++) {
417  node[i].aperture = thickness;
418  }
419  }
420 
421  printf("\n----------------FLOW SOLUTION DATA READING--------------------\n");
422  fehm = 0;
423  pflotran = 0;
424  inputfile = Control_File("FEHM:", 5);
425  res = strncmp(inputfile.filename, "yes", 3);
426 
427  if (res == 0) {
428  ReadFEHMfile(nedges);
429  fehm = 1;
430  } else {
431  inputfile = Control_File("PFLOTRAN:", 9);
432  res = strncmp(inputfile.filename, "yes", 3);
433 
434  if (res == 0) {
435  ReadPFLOTRANfile(nedges);
436  pflotran = 1;
437  } else {
438  printf("\n FLOW SOLUTION SOURCE IS NOT DEFINED. Program is terminated. \n");
439  exit(1);
440  }
441  }
442 
443  /***** ordering neighboring nodes, fluxes and area coefficients ******/
444  for (i = 0; i < nnodes; i++) {
445  node[i].numneighb = (node[i].numneighb) - 1;
446  l = 0;
447 
448  for (j = 0; j < (node[i].numneighb) + 1; j++) {
449  if (node[i].indnodes[j] != i + 1) {
450  node[i].indnodes[l] = node[i].indnodes[j];
451  node[i].type[l] = node[node[i].indnodes[l] - 1].typeN;
452  node[i].flux[l] = node[i].flux[j];
453 
454  if (node[i].area[j] < 0) {
455  node[i].area[l] = node[i].area[j] * (-1.0);
456  } else {
457  node[i].area[l] = node[i].area[j];
458  }
459 
460  for (k = 0; k < 4; k++) {
461  node[i].cells[l][k] = 0;
462  node[i].fracts[l][k] = 0;
463  }
464 
465  l++;
466  }
467  }
468  }
469 
470  /***** search for neighboring cells *******************/
471 
472  for (ln = 0; ln < ncells; ln++) {
473  cell[ln].veloc_ind[0] = 0;
474  cell[ln].veloc_ind[1] = 0;
475  cell[ln].veloc_ind[2] = 0;
476  AdjacentCells(ln, cell[ln].node_ind[0], cell[ln].node_ind[1], cell[ln].node_ind[2]);
477  AdjacentCells(ln, cell[ln].node_ind[1], cell[ln].node_ind[2], cell[ln].node_ind[0]);
478  AdjacentCells(ln, cell[ln].node_ind[2], cell[ln].node_ind[0], cell[ln].node_ind[1]);
479  } //loop on ln
480 
481  return;
482 }
484 
485 void AdjacentCells(int ln, int in, int jn, int kn)
487 {
488  int jj, kk;
489 
490  for(jj = 0; jj < node[in - 1].numneighb; jj++) {
491  if (node[in - 1].indnodes[jj] == jn) {
492  for (kk = 0; kk < 4; kk++) {
493  if (node[in - 1].cells[jj][kk] == 0) {
494  node[in - 1].cells[jj][kk] = ln + 1;
495  node[in - 1].fracts[jj][kk] = cell[ln].fracture;
496 
497  if (cell[ln].fracture != node[in - 1].fracture[0]) {
498  node[in - 1].fracture[1] = cell[ln].fracture;
499  }
500 
501  break;
502  }
503  }
504  }
505 
506  if (node[in - 1].indnodes[jj] == kn) {
507  for (kk = 0; kk < 4; kk++) {
508  if (node[in - 1].cells[jj][kk] == 0) {
509  node[in - 1].cells[jj][kk] = ln + 1;
510  node[in - 1].fracts[jj][kk] = cell[ln].fracture;
511 
512  if (cell[ln].fracture != node[in - 1].fracture[0]) {
513  node[in - 1].fracture[1] = cell[ln].fracture;
514  }
515 
516  break;
517  }
518  }
519  }
520  } //loop on jj
521 
522  return;
523 }
525 
528 {
529  struct inpfile inputfile;
530  inputfile = Control_File("boundary:", 9 );
531  printf("\n OPEN AND READ FILE: %s \n ", inputfile.filename);
532  printf("\n Read the number and indices of nodes defined in flow in and flow out zones \n");
533  FILE *fpc = OpenFile (inputfile.filename, "r");
534  int zonenumb_in = 0, zonenumb_out = 0;
535  /* input from control.dat file */
536  inputfile = Control_Data("in-flow-boundary:", 17 );
537  zonenumb_in = inputfile.flag;
538  inputfile = Control_Data("out-flow-boundary:", 18 );
539  zonenumb_out = inputfile.flag;
540  char filename[125];
541  char line[10] = {0};
542  int nzone_out, i, fn, nn, res, nf, flag1 = 0, flag2 = 0;
543 
544  if (fscanf(fpc, "%s \n", line) != 1) {
545  i = i;
546  }
547 
548  do {
549  fn = 0;
550 
551  if (fscanf(fpc, " %d %s \n", &fn, line) != 2) {
552  fn = fn;
553  }
554 
555  if (fn == zonenumb_in) {
556  flag1 = 1;
557 
558  if (fscanf(fpc, " %s \n ", line) != 1) {
559  flag1 = flag1;
560  }
561 
562  if (fscanf(fpc, " %d", &nzone_in) != 1) {
563  nzone_in = nzone_in;
564  }
565 
566  /***** memory allocation for nodezone_in nodes ******/
567  nodezonein = (unsigned int*) malloc (nzone_in * sizeof(unsigned int));
568 
569  for (i = 0; i < nzone_in; i++) {
570  if (fscanf(fpc, " %d ", &nf) != 1) {
571  i = i;
572  }
573 
574  nodezonein[i] = nf;
575 
576  if(node[nodezonein[i] - 1].typeN < 300) {
577  node[nodezonein[i] - 1].typeN = node[nodezonein[i] - 1].typeN + 300;
578  }
579  }
580  } else {
581  if (fn == zonenumb_out) {
582  flag2 = 1;
583 
584  if (fscanf(fpc, " %s ", line) != 1) {
585  fn = fn;
586  }
587 
588  if (fscanf(fpc, " %d", &nzone_out) != 1) {
589  nzone_out = nzone_out;
590  }
591 
592  /***** memory allocation for nodezoneout nodes ******/
593  nodezoneout = (unsigned int*) malloc (nzone_out * sizeof(unsigned int));
594 
595  for (i = 0; i < nzone_out; i++) {
596  if (fscanf(fpc, " %d ", &nf) != 1) {
597  nf = nf;
598  }
599 
600  nodezoneout[i] = nf;
601 
602  if(node[nodezoneout[i] - 1].typeN < 200) {
603  node[nodezoneout[i] - 1].typeN = node[nodezoneout[i] - 1].typeN + 200;
604  }
605  }
606  } else {
607  if (fscanf(fpc, " %s ", line) != 1) {
608  nn = nn;
609  }
610 
611  if (fscanf(fpc, " %d ", &nn) != 1) {
612  nn = nn;
613  }
614 
615  for (i = 0; i < nn; i++) {
616  if (fscanf(fpc, " %d ", &nf) != 1) {
617  nf = nf;
618  }
619  }
620  }
621  }
622  } while ((res = strncmp(line, "stop", 4)));
623 
624  if ((flag1 = 0) || (flag2 = 0)) {
625  printf("Please check zones in boundary zone file.\n");
626  exit(1);
627  }
628 
629  printf("\n Number of nodes %d in flow-in zone. \n", nzone_in);
630  printf("\n Number of nodes %d in flow-out zone. \n", nzone_out);
631  fclose(fpc);
632  /*** calculate the sum of mass fluxes on in-flow boundary and on out-flow boundary
633  ***/
634  unsigned int j;
635  double sum_in = 0, sum_out = 0.0;
636 
637  for (i = 0; i < nzone_in; i++) {
638  for (j = 0; j < node[nodezonein[i] - 1].numneighb; j++) {
639  sum_in = sum_in + node[nodezonein[i] - 1].flux[j];
640  }
641  }
642 
643  totalFluxIn = sum_in / density;
644  printf ("Total in-flow volumetric flux = %12.5e [m^3/s] \n", totalFluxIn);
645  FILE *fluxin;
646  sprintf(filename, "%s/inputflux_m3s", maindir);
647  fluxin = OpenFile(filename, "w");
648  fprintf(fluxin, "%12.5e\n", totalFluxIn);
649  fclose(fluxin);
650 
651  for (i = 0; i < nzone_out; i++) {
652  for (j = 0; j < node[nodezoneout[i] - 1].numneighb; j++) {
653  sum_out = sum_out + node[nodezoneout[i] - 1].flux[j];
654  }
655  }
656 
657  sum_out = sum_out / density;
658  printf ("Total out-flow volumetric flux = %12.5e [m^3/s]\n", sum_out);
659  /***************************/
660  return;
661 }
663 
664 FILE *OpenFile(char filen[120], char fileopt[2])
666 {
667  FILE* filepointer;
668 
669  if ((filepointer = fopen (filen, fileopt)) == NULL) {
670  printf ("File %s could not be opened. Program is terminated. \n", filen);
671  exit(1);
672  }
673 
674  return filepointer;
675 }
677 void ReadPFLOTRANfile(int nedges)
679 {
680  struct inpfile inputfile;
681  int n1 = 0, n2 = 0, i, n_flux, j, k, res1;
682  char wordc[60];
683  int n_area;
684  double floatnumber, areavalue;
685  double length;
686  density = 1.0;
687  inputfile = Control_File("PFLOTRAN_vel:", 13 );
688  FILE *pf = OpenFile (inputfile.filename, "r");
689  printf("\n PFLOTRAN: OPEN AND READ FILE: %s \n \n", inputfile.filename);
690  // reading fluxes
691  double l_flux = 0.0;
692  double l_dens = 0.0;
693  double l_area = 0.0;
694  char cs, csp;
695  n1 = 0;
696  n2 = 0;
697  int flag = 0;
698  n_flux = (nedges - nnodes) / 2.;
699  // check the darcyvel file format;
700  csp = ' ';
701 
702  do {
703  cs = fgetc(pf);
704 
705  if ((cs != ' ') && (csp == ' ')) {
706  n2++;
707  }
708 
709  csp = cs;
710  } while (cs != '\n');
711 
712  if (n2 > 5) {
713  flag = 1;
714  }
715 
716  // if the old darcy velocity format is used, the uge file is read for area coefficients
717 
718  if (flag == 0) {
719  printf(" THE VELOCITY FILE FORMAT IS INCORRECT \n");
720  exit(1);
721  }
722 
723  /********************/
724  n2 = 0;
725  rewind(pf);
726 
727  for (i = 0; i < n_flux; i++) {
728  if (flag == 0) {
729  if (fscanf(pf, " %d %d %lf %lf ", &n1, &n2, &l_flux, &l_dens) != 4) {
730  printf("Error");
731  }
732  } else if (fscanf(pf, " %d %d %lf %lf %lf ", &n1, &n2, &l_flux, &l_dens, &l_area) != 5) {
733  printf("Error");
734  }
735 
736  for (j = 0; j < node[n1 - 1].numneighb; j++) {
737  if (node[n1 - 1].indnodes[j] == n2) {
738  if (flag != 0) {
739  node[n1 - 1].area[j] = l_area;
740  }
741 
742  node[n1 - 1].flux[j] = l_flux * (node[n1 - 1].area[j]);
743 
744  for (k = 0; k < node[n2 - 1].numneighb; k++) {
745  if (node[n2 - 1].indnodes[k] == n1) {
746  if (flag != 0) {
747  node[n2 - 1].area[k] = l_area;
748  }
749 
750  node[n2 - 1].flux[k] = l_flux * (-1.0) * node[n2 - 1].area[k];
751  break;
752  }
753  }
754 
755  break;
756  }
757  }
758  }
759 
760  fclose(pf);
761  inputfile = Control_File("PFLOTRAN_cell:", 14 );
762  FILE *fp = OpenFile (inputfile.filename, "r");
763  printf("\n PFLOTRAN: OPEN AND READ FILE: %s \n \n", inputfile.filename);
764  //reading pressure
765  printf(" Reading pressure \n");
766  double maxpr = 0.0, minpr = 100.0, l_pres = 0.0;
767 
768  for (i = 0; i < nnodes; i++) {
769  if (fscanf(fp, " %d %lf %lf %lf %lf \n", &n1, &l_flux, &l_flux, &l_dens, &l_pres) != 5) {
770  printf("Error");
771  }
772 
773  node[n1 - 1].pressure = l_pres / pow(10, 6);
774 
775  if (node[n1 - 1].pressure > maxpr) {
776  maxpr = node[n1 - 1].pressure;
777  }
778 
779  if (node[n1 - 1].pressure < minpr) {
780  minpr = node[n1 - 1].pressure;
781  }
782  }
783 
784  fclose(fp);
785  printf(" MAX pressure %5.8e [MPa]; MIN pressure %5.8e [MPa] \n", maxpr, minpr);
786 
787  // update cell volumes according to aperture
788 
789  for (i = 0; i < nnodes; i++) {
790  node[i].pvolume = node[i].pvolume * node[i].aperture;
791  }
792 
793  return;
794 }
796 void ReadFEHMfile(int nedges)
798 {
799  int i, j;
800  char cs, line[16];
801  struct inpfile inputfile;
802  inputfile = Control_File("FEHM_fin:", 9 );
803  FILE *fpr = OpenFile (inputfile.filename, "r");
804  printf("\n FEHM: OPEN AND READ FILE: %s \n \n", inputfile.filename);
805 
806  /* Read the head of the file */
807  for (i = 0; i < 4; i++) {
808  do {
809  cs = fgetc(fpr);
810  } while (cs != '\n');
811  }
812 
813  unsigned int par_flag = 0;
814 
815  /* reading the pressure on nodes , Pressure in MPa units*/
816  do {
817  if (fscanf(fpr, "%s \n", line) != 1) {
818  i = i;
819  }
820 
821  double maxpr = 0.0, minpr = 100.0;
822  int res = strncmp(line, "pressure", 8);
823 
824  if (res == 0) {
825  par_flag = 1;
826  printf(" Reading %s\n", line);
827 
828  for (i = 0; i < nnodes; i++) {
829  if (fscanf(fpr, "%lf", &node[i].pressure) != 1) {
830  i = i;
831  }
832 
833  if (node[i].pressure > maxpr) {
834  maxpr = node[i].pressure;
835  }
836 
837  if (node[i].pressure < minpr) {
838  minpr = node[i].pressure;
839  }
840  }
841 
842  printf(" MAX pressure %5.8e MIN pressure %5.8e \n", maxpr, minpr);
843  }
844  } while ((par_flag != 1) || (fgets(line, 16, fpr) != NULL));
845 
846  if (par_flag == 0) {
847  printf(" !!! THERE IS NO PRESSURE DATA !!! \n");
848  }
849 
850  /* reading fluxes on edges*/
851  rewind(fpr);
852  par_flag = 0;
853 
854  do {
855  if (fscanf(fpr, "%s", line) != 1) {
856  i = i;
857  }
858 
859  int res1 = strncmp(line, "flux", 4);
860 
861  if (res1 == 0) {
862  par_flag = 1;
863  printf("\n Reading %s\n", line);
864 
865  if (fscanf(fpr, "%d\n", &i) != 1) {
866  i = i;
867  }
868 
869  if (nedges != i) {
870  printf("number of edges in *.fin file (%d )is not the same as number of edges in *.stor file (%d)!\n", i, nedges);
871  }
872 
873  /* reading fluxes */
874 
875  for (i = 0; i < nnodes; i++) {
876  for (j = 0; j < max_neighb; j++) {
877  if (j < node[i].numneighb) {
878  if( fscanf(fpr, "%lf", &node[i].flux[j]) != 1) {
879  i = i;
880  }
881  }
882  }
883  }
884 
885  printf("\n Fluxes are read from FEHM file \n");
886  }
887  } while ((par_flag != 1) || (fgets(line, 16, fpr) != NULL));
888 
889  if (par_flag == 0) {
890  printf(" !!! THERE IS NO FLUX DATA !!! \n");
891  }
892 
893  fclose(fpr);
894  return;
895 }
896 
902 {
903  char filename[125];
904  sprintf(filename, "%s/nodes", maindir);
905  FILE *wp = OpenFile (filename, "w");
906  printf("\n Output initial data structure \n");
907  /************* output to file node ************************/
908  int i, j, k;
909 
910  for (i = 0; i < nnodes; i++) {
911  fprintf(wp, "\n Node %d, type %d, x=%5.8e, y=%5.8e, z=%5.8e, aperture=%5.8e, \n", i + 1,
912  node[i].typeN, node[i].coord[0], node[i].coord[1], node[i].coord[2], node[i].aperture);
913  fprintf(wp, " Fracture(s) %d %d \n", node[i].fracture[0], node[i].fracture[1]);
914 
915  if (node[i].fracture[1] == 0) {
916  fprintf(wp, " Vx %5.8e Vy %5.8e \n", node[i].velocity[0][0], node[i].velocity[0][1] );
917  } else {
918  fprintf(wp, " Vx %5.8e Vy %5.8e \n", node[i].velocity[0][0], node[i].velocity[0][1] );
919  fprintf(wp, " Vx %5.8e Vy %5.8e \n", node[i].velocity[1][0], node[i].velocity[1][1] );
920  fprintf(wp, " Vx %5.8e Vy %5.8e \n", node[i].velocity[2][0], node[i].velocity[2][1] );
921  fprintf(wp, " Vx %5.8e Vy %5.8e \n", node[i].velocity[3][0], node[i].velocity[3][1] );
922  }
923 
924  fprintf(wp, " Pressure %5.8e, Volume %5.8e , Connections %d \n",
925  node[i].pressure, node[i].pvolume, node[i].numneighb);
926 
927  for (j = 0; j < node[i].numneighb; j++) {
928  fprintf(wp, " Face %d %d (type %d), flux %5.8e, area %5.8e, cells:\n",
929  i + 1, node[i].indnodes[j], node[i].type[j], node[i].flux[j], node[i].area[j]);
930 
931  for (k = 0; k < 4; k++) {
932  if (node[i].cells[j][k] != 0)
933  fprintf(wp, "(%d) %d in fracture %d; ", k + 1,
934  node[i].cells[j][k], node[i].fracts[j][k]);
935  }
936 
937  fprintf(wp, "\n");
938  } //loop j
939  } //loop i
940 
941  fclose(wp);
942  // printf("\n Node's init data is written into file \n");
943  /* Fracture's structure includes indices of cells that belong to fructure and three nodes numbers*/
944  sprintf(filename, "%s/fractures", maindir);
945  FILE *wf = OpenFile (filename, "w");
946 
947  for (i = 0; i < nfract; i++) {
948  fprintf(wf, "\n Fracture %d first node# %d last node# %d and theta %f\n",
949  i + 1, fracture[i].firstnode, fracture[i].lastnode, fracture[i].theta);
950  fprintf(wf, "\n Fracture %d has %d cells, starting from %d \n", i + 1,
951  fracture[i].numbcells, fracture[i].firstcell);
952 
953  for (j = 0; j < fracture[i].numbcells; j++) {
954  fprintf(wf, " cell %d, nodes: %d %d %d \n", fracture[i].firstcell + j,
955  cell[fracture[i].firstcell + j - 1].node_ind[0],
956  cell[fracture[i].firstcell + j - 1].node_ind[1],
957  cell[fracture[i].firstcell + j - 1].node_ind[2]);
958  }//loop j
959  } //loop i
960 
961  fclose(wf);
962  return;
963 }
965 void CheckGrid()
969 {
970  int i, j, k, r = 0;
971  printf(" GRID CHECK starts \n");
972 
973  for (i = 0; i < nnodes; i++) {
974  if ((node[i].typeN == 0) || (node[i].typeN == 10)) {
975  r = 0;
976 
977  for (j = 0; j < node[i].numneighb; j++) {
978  for (k = 0; k < 4; k++) {
979  if ((node[i].fracts[j][k] != node[i].fracture[0]) && (node[i].fracts[j][k] != 0)) {
980  printf("GRID ERROR: fracture %d node %d / fracture %d \n", node[i].fracture[0], i + 1, node[i].fracts[j][k]);
981  printf(" node %d %lf %lf %lf \n", i + 1, node[i + 1].coord[0], node[i + 1].coord[1], node[i + 1].coord[2]);
982  r++;
983  break;
984  }
985  }
986 
987  if (r > 0) {
988  break;
989  }
990  }
991  }
992  }
993 
994  printf(" GRID CHECK is done \n");
995  return;
996 }
998 struct inpfile Control_File(char fileobject[], int ctr)
1001 {
1002  FILE *cf = OpenFile(controlfile, "r");
1003  struct inpfile inputfile;
1004  char fileline[120];
1005  char* fileend = "END";
1006  int end = 0;
1007  inputfile.param = 0.0;
1008  inputfile.flag = -1;
1009  int res2 = 0, res1 = 0;
1010 
1011  do {
1012  if (fscanf(cf, "\n %s", fileline) != 1) {
1013  printf("ERROR");
1014  }
1015 
1016  end = 0;
1017  res2 = String_Compare(fileline, fileend);
1018 
1019  if (res2 == 0) {
1020  end = strlen(fileend);
1021  }
1022 
1023  res1 = String_Compare(fileline, fileobject);
1024 
1025  if (res1 == 0) {
1026  if (fscanf(cf, "%s", inputfile.filename) != 1) {
1027  printf("Error");
1028  }
1029 
1030  inputfile.flag = 1;
1031  end = strlen(fileend);
1032  }
1033  } while (end != strlen(fileend));
1034 
1035  if (inputfile.flag < 0) {
1036  printf("\n There is no %s input found in %s. Program is terminated. \n", fileobject, controlfile);
1037  exit(1);
1038  }
1039 
1040  fclose(cf);
1041  return inputfile;
1042 }
1043 
1045 struct inpfile Control_File_Optional(char fileobject[], int ctr)
1049 {
1050  FILE *cf = OpenFile(controlfile, "r");
1051  struct inpfile inputfile;
1052  char fileline[120];
1053  char* fileend = "END";
1054  int end = 0;
1055  inputfile.param = 0.0;
1056  inputfile.flag = -1;
1057  int res2 = 0, res1 = 0;
1058 
1059  do {
1060  if (fscanf(cf, "\n %s", fileline) != 1) {
1061  printf("ERROR");
1062  }
1063 
1064  end = 0;
1065  res2 = String_Compare(fileline, fileend);
1066 
1067  if (res2 == 0) {
1068  end = strlen(fileend);
1069  }
1070 
1071  res1 = String_Compare(fileline, fileobject);
1072 
1073  if (res1 == 0) {
1074  if (fscanf(cf, "%s", inputfile.filename) != 1) {
1075  printf("Error");
1076  }
1077 
1078  inputfile.flag = 1;
1079  end = strlen(fileend);
1080  }
1081  } while (end != strlen(fileend));
1082 
1083  fclose(cf);
1084  return inputfile;
1085 }
1086 
1087 
1089 int String_Compare(char string1[], char string2[])
1091 {
1092  int resultc = 1, res = 0, k = 0;
1093 
1094  for (res = 0; res < strlen(string2); res++) {
1095  if (string1[res] != string2[res]) {
1096  break;
1097  } else {
1098  k++;
1099  }
1100  }
1101 
1102  if (k == strlen(string2)) {
1103  resultc = 0;
1104  }
1105 
1106  return resultc;
1107 }
1108 
1109 
1111 
1112 struct inpfile Control_Data(char fileobject[], int ctr)
1115 {
1116  FILE *cf = OpenFile(controlfile, "r");
1117  struct inpfile inputfile;
1118  char fileline[120];
1119  int end = 0;
1120  char* fileend = "END";
1121  inputfile.flag = -1;
1122  inputfile.param = 0.0;
1123  int res2 = 0, res1 = 0;
1124 
1125  do {
1126  if (fscanf(cf, "%s", fileline) != 1) {
1127  printf("ERROR");
1128  }
1129 
1130  end = 0;
1131  res2 = String_Compare(fileline, fileend);
1132 
1133  if (res2 == 0) {
1134  end = strlen(fileend);
1135  }
1136 
1137  res1 = String_Compare(fileline, fileobject);
1138 
1139  if (res1 == 0) {
1140  if (fscanf(cf, "%ld", &inputfile.flag) != 1) {
1141  printf("Error");
1142  }
1143 
1144  end = strlen(fileend);
1145  }
1146  } while (end != strlen(fileend));
1147 
1148  if (inputfile.flag < 0) {
1149  printf("\n There is no %s input found in %s. Program is terminated. \n", fileobject, controlfile);
1150  exit(1);
1151  }
1152 
1153  fclose(cf);
1154  return inputfile;
1155 }
1157 struct inpfile Control_Param(char fileobject[], int ctr)
1160 {
1161  FILE *cf = OpenFile(controlfile, "r");
1162  struct inpfile inputfile;
1163  char fileline[120];
1164  char* fileend = "END";
1165  int end = 0;
1166  inputfile.param = 0.0;
1167  inputfile.flag = -1;
1168  int res2 = 0, res1 = 0;
1169 
1170  do {
1171  if (fscanf(cf, "%s", fileline) != 1) {
1172  printf("ERROR");
1173  }
1174 
1175  end = 0;
1176  res2 = String_Compare(fileline, fileend);
1177 
1178  if (res2 == 0) {
1179  end = strlen(fileend);
1180  }
1181 
1182  res1 = String_Compare(fileline, fileobject);
1183 
1184  if (res1 == 0) {
1185  if (fscanf(cf, "%lf", &inputfile.param) != 1) {
1186  printf("Error");
1187  }
1188 
1189  inputfile.flag = 1;
1190  end = strlen(fileend);
1191  }
1192  } while (end != strlen(fileend));
1193 
1194  if (inputfile.flag < 0) {
1195  printf("\n There is no %s input found in %s. Program is terminated. \n", fileobject, controlfile);
1196  exit(1);
1197  }
1198 
1199  fclose(cf);
1200  return inputfile;
1201 }
1205 {
1206  struct inpfile inputfile;
1207  char cs;
1208  int i, res_a;
1209  inputfile = Control_File("aperture_type:", 14);
1210  res_a = strncmp(inputfile.filename, "frac", 4);
1211 
1212  // if given aperture is const per fracture
1213  if (res_a == 0) {
1214  inputfile = Control_File("aperture_file:", 14 );
1215  FILE *ad = OpenFile (inputfile.filename, "r");
1216  printf("\n OPEN AND READ FILE: %s \n ", inputfile.filename);
1217  double currentap = 0.0, aperturem[nfract];
1218  int apmat = 0, zn;
1219 
1220  do {
1221  cs = fgetc(ad);
1222  } while (cs != '\n');
1223 
1224  for (i = 0; i < nfract; i++) {
1225  if (fscanf(ad, "%d %d %d %lf \n", &apmat, &zn, &zn, &currentap) != 4) {
1226  printf("Error");
1227  }
1228 
1229  apmat = apmat * (-1) - 6;
1230  aperturem[apmat - 1] = currentap;
1231  }
1232 
1233  fclose(ad);
1234 
1235  for (i = 0; i < nnodes; i++) {
1236  // define an aperture of the cell with current node as cellcenter
1237  if (node[i].fracture[1] == 0) {
1238  node[i].aperture = aperturem[node[i].fracture[0] - 1];
1239  } else {
1240  if (aperturem[node[i].fracture[0] - 1] >= aperturem[node[i].fracture[1] - 1]) {
1241  node[i].aperture = aperturem[node[i].fracture[0] - 1];
1242  } else {
1243  node[i].aperture = aperturem[node[i].fracture[1] - 1];
1244  }
1245  }
1246  }
1247  } else {
1248  res_a = strncmp(inputfile.filename, "cell", 4);
1249 
1250  // if given aperture is given per cell
1251  if (res_a == 0) {
1252  inputfile = Control_File("aperture_file:", 14 );
1253  FILE *ada = OpenFile (inputfile.filename, "r");
1254  printf("\n OPEN AND READ FILE: %s \n ", inputfile.filename);
1255  double currentap = 0.0;
1256  double *aperturem;
1257  aperturem = (double*)malloc(nnodes * sizeof(double));
1258  int apmat = 0;
1259  char c;
1260 
1261  do {
1262  c = fgetc(ada);
1263  } while (c != '\n');
1264 
1265  for (i = 0; i < nnodes; i++) {
1266  if(fscanf(ada, "%d %lf \n", &apmat, &currentap) != 2) {
1267  printf("Error");
1268  }
1269 
1270  aperturem[apmat - 1] = currentap;
1271  }
1272 
1273  fclose(ada);
1274 
1275  for (i = 0; i < nnodes; i++) {
1276  // define an aperture of the cell with current node as cellcenter
1277  node[i].aperture = aperturem[i];
1278  }
1279 
1280  free(aperturem);
1281  }
1282  }
1283 
1284  return;
1285 }
unsigned int nnodes
Definition: main.c:29
double thickness
Definition: main.c:49
struct element * cell
Definition: main.c:42
char maindir[125]
Definition: main.c:43
unsigned int ncells
Definition: main.c:30
unsigned int nzone_in
Definition: main.c:35
double density
Definition: main.c:47
unsigned int pflotran
Definition: main.c:27
unsigned int fehm
Definition: main.c:28
unsigned int * nodezoneout
Definition: main.c:37
unsigned int max_neighb
Definition: main.c:32
struct vertex * node
Definition: main.c:40
double totalFluxIn
Definition: main.c:52
char controlfile[120]
Definition: main.c:44
struct material * fracture
Definition: main.c:39
unsigned int nfract
Definition: main.c:31
unsigned int * nodezonein
Definition: main.c:36
int String_Compare(char string1[], char string2[])
FILE * OpenFile(char filen[120], char fileopt[2])
Definition: ReadGridInit.c:664
struct inpfile Control_File(char fileobject[], int ctr)
Definition: ReadGridInit.c:998
void ReadPFLOTRANfile(int nedges)
Definition: ReadGridInit.c:677
void ReadInit()
Definition: ReadGridInit.c:16
struct inpfile Control_File_Optional(char fileobject[], int ctr)
void ReadDataFiles()
Definition: ReadGridInit.c:155
struct inpfile Control_Param(char fileobject[], int ctr)
void AdjacentCells(int ln, int in, int jn, int kn)
Definition: ReadGridInit.c:485
void ReadAperture()
struct inpfile Control_Data(char fileobject[], int ctr)
void ReadFEHMfile(int nedges)
Definition: ReadGridInit.c:796
void CheckGrid()
Definition: ReadGridInit.c:965
void ReadBoundaryNodes()
Definition: ReadGridInit.c:526
void WritingInit()
Definition: ReadGridInit.c:898
unsigned int veloc_ind[3]
Definition: FuncDef.h:170
unsigned int fracture
Definition: FuncDef.h:164
char filename[120]
unsigned int firstcell
Definition: FuncDef.h:92
float nvect_xy[2]
Definition: FuncDef.h:80
unsigned int firstnode
Definition: FuncDef.h:86
unsigned int numbcells
Definition: FuncDef.h:95
unsigned int lastnode
Definition: FuncDef.h:89
float nvect_z[2]
Definition: FuncDef.h:83
float theta
Definition: FuncDef.h:77
double pressure
Definition: FuncDef.h:124
unsigned int ** cells
Definition: FuncDef.h:139
double aperture
Definition: FuncDef.h:157
double pvolume
Definition: FuncDef.h:121
unsigned int * indnodes
Definition: FuncDef.h:136
double * flux
Definition: FuncDef.h:148
unsigned int fracture[2]
Definition: FuncDef.h:112
unsigned int typeN
Definition: FuncDef.h:109
unsigned int numneighb
Definition: FuncDef.h:127
unsigned int ** fracts
Definition: FuncDef.h:142
unsigned int * type
Definition: FuncDef.h:145
double * area
Definition: FuncDef.h:151