FrontISTR  5.2.0
Large-scale structural analysis program with finit element method
hecmw_result_txt_io.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 #include "hecmw_util.h"
11 #include "hecmw_result.h"
12 #include "hecmw_result_io.h"
13 
14 /*---------------------------------------------------------------------------*/
15 /* TEXT MODE I/O ---- output_result */
16 /*---------------------------------------------------------------------------*/
17 
18 
19 static int output_result_header(FILE *fp) {
20  int rc;
21 
22  /* header */
23  if( filever_major > 1 ){
24  sprintf(head,"%s %d.%d",head,filever_major,filever_minor);
25  }
26  rc = fprintf(fp, "%s\n", head);
27  if(rc < 0) {
29  return -1;
30  }
31 
32  return 0;
33 }
34 
35 
36 static int output_result_global(FILE *fp) {
37  int i,j,k,n,rc,ng_comp;
38  struct result_list *p,**data;
39 
40  /* comment */
41  rc = fprintf(fp, "*comment\n");
42  if(rc < 0) {
43  HECMW_set_error(HECMW_UTIL_E0205, "*comment");
44  return -1;
45  }
46  rc = fprintf(fp, "%s\n", comment_line);
47  if(rc < 0) {
49  return -1;
50  }
51 
52  /* global header */
53  rc = fprintf(fp, "*global\n");
54  if(rc < 0) {
56  return -1;
57  }
58 
59  /* ng_component */
60  rc = fprintf(fp, "%d\n", HECMW_result_count_ng_comp());
61  if(rc < 0) {
63  return -1;
64  }
65 
66  /* ng_dof */
67  n = 0;
68  for(p=global_list; p; p=p->next) {
69  rc = fprintf(fp, "%d%c", p->n_dof, (n+1)%COL_INT ? ' ' : '\n');
70  if(rc < 0) {
72  return -1;
73  }
74  n++;
75  }
76  if(n % COL_INT) {
77  rc = fprintf(fp, "\n");
78  if(rc < 0) {
80  return -1;
81  }
82  }
83 
84  /* global_label */
85  for(p=global_list; p; p=p->next) {
86  rc = fprintf(fp, "%s\n", p->label);
87  if(rc < 0) {
88  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
89  return -1;
90  }
91  }
92 
93  /* global_val_item */
94  ng_comp = HECMW_result_count_ng_comp();
95  if(ng_comp == 0) return 0;
96  data = HECMW_malloc(sizeof(*data) * ng_comp);
97  if(data == NULL) {
98  HECMW_set_error(errno, "");
99  return -1;
100  }
101  i = 0;
102  for(p=global_list; p; p=p->next) {
103  data[i++] = p;
104  }
105  n = 0;
106  for(j=0; j < ng_comp; j++) {
107  p = data[j];
108  for(k=0; k < p->n_dof; k++) {
109  rc = fprintf(fp, "%.16E%c", p->ptr[k], (n+1)%COL_DOUBLE ? ' ' : '\n');
110  if(rc < 0) {
111  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
112  return -1;
113  }
114  n++;
115  }
116  }
117  if(n % COL_DOUBLE) {
118  rc = fprintf(fp, "\n");
119  if(rc < 0) {
121  return -1;
122  }
123  }
124  HECMW_free(data);
125 
126  return 0;
127 }
128 
129 
130 static int output_result_dataheader(FILE *fp) {
131  int rc;
132 
133  /* n_node, n_elem */
134  rc = fprintf(fp, "%d %d\n", nnode, nelem);
135  if(rc < 0) {
136  HECMW_set_error(HECMW_UTIL_E0205, "nnode,nelem");
137  return -1;
138  }
139 
140  /* nn_component, ne_component */
141  rc = fprintf(fp, "%d %d\n", HECMW_result_count_nn_comp(), HECMW_result_count_ne_comp());
142  if(rc < 0) {
143  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
144  return -1;
145  }
146 
147  return 0;
148 }
149 
150 
151 static int output_result_node(FILE *fp) {
152  int i,j,k,n,rc,nn_comp;
153  struct result_list *p,**data;
154 
155  /* nn_dof */
156  n = 0;
157  for(p=node_list; p; p=p->next) {
158  rc = fprintf(fp, "%d%c", p->n_dof, (n+1)%COL_INT ? ' ' : '\n');
159  if(rc < 0) {
161  return -1;
162  }
163  n++;
164  }
165  if(n % COL_INT) {
166  rc = fprintf(fp, "\n");
167  if(rc < 0) {
169  return -1;
170  }
171  }
172 
173  /* node_label */
174  for(p=node_list; p; p=p->next) {
175  rc = fprintf(fp, "%s\n", p->label);
176  if(rc < 0) {
177  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
178  return -1;
179  }
180  }
181 
182  /* node_val_item */
183  nn_comp = HECMW_result_count_nn_comp();
184  if(nn_comp == 0) return 0;
185  data = HECMW_malloc(sizeof(*data) * nn_comp);
186  if(data == NULL) {
187  HECMW_set_error(errno, "");
188  return -1;
189  }
190  i = 0;
191  for(p=node_list; p; p=p->next) {
192  data[i++] = p;
193  }
194  for(i=0; i < nnode; i++) {
195  rc = fprintf(fp, "%d \n", node_global_ID[i]);
196  if(rc < 0) {
197  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
198  return -1;
199  }
200  n = 0;
201  for(j=0; j < nn_comp; j++) {
202  p = data[j];
203  for(k=0; k < p->n_dof; k++) {
204  rc = fprintf(fp, "%.16E%c", p->ptr[i*p->n_dof+k], (n+1)%COL_DOUBLE ? ' ' : '\n');
205  if(rc < 0) {
206  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
207  return -1;
208  }
209  n++;
210  }
211  }
212  if(n % COL_DOUBLE) {
213  rc = fprintf(fp, "\n");
214  if(rc < 0) {
216  return -1;
217  }
218  }
219  }
220  HECMW_free(data);
221 
222  return 0;
223 }
224 
225 
226 static int output_result_elem(FILE *fp) {
227  int i,j,k,n,rc,ne_comp;
228  struct result_list *p,**data;
229 
230  /* ne_dof */
231  n = 0;
232  for(p=elem_list; p; p=p->next) {
233  rc = fprintf(fp, "%d%c", p->n_dof, (n+1)%COL_INT ? ' ' : '\n');
234  if(rc < 0) {
236  return -1;
237  }
238  n++;
239  }
240  if(n % COL_INT) {
241  rc = fprintf(fp, "\n");
242  if(rc < 0) {
244  return -1;
245  }
246  }
247 
248  /* elem_label */
249  for(p=elem_list; p; p=p->next) {
250  rc = fprintf(fp, "%s\n", p->label);
251  if(rc < 0) {
252  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
253  return -1;
254  }
255  }
256 
257  /* elem_val_item */
258  ne_comp = HECMW_result_count_ne_comp();
259  if(ne_comp == 0) return 0;
260  data = HECMW_malloc(sizeof(*data) * ne_comp);
261  if(data == NULL) {
262  HECMW_set_error(errno, "");
263  return -1;
264  }
265  i = 0;
266  for(p=elem_list; p; p=p->next) {
267  data[i++] = p;
268  }
269  for(i=0; i < nelem; i++) {
270  rc = fprintf(fp, "%d\n", elem_global_ID[i]);
271  if(rc < 0) {
272  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
273  return -1;
274  }
275  n = 0;
276  for(j=0; j < ne_comp; j++) {
277  p = data[j];
278  for(k=0; k < p->n_dof; k++) {
279  rc = fprintf(fp, "%.16E%c", p->ptr[i*p->n_dof+k], (n+1)%COL_DOUBLE ? ' ' : '\n');
280  if(rc < 0) {
281  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
282  return -1;
283  }
284  n++;
285  }
286  }
287  if(n % COL_DOUBLE) {
288  rc = fprintf(fp, "\n");
289  if(rc < 0) {
291  return -1;
292  }
293  }
294  }
295  HECMW_free(data);
296 
297  return 0;
298 }
299 
300 
301 static int output_result_data(FILE *fp) {
302  int rc;
303  HECMW_assert(fp);
304 
305  if(output_result_header(fp)) {
306  return -1;
307  }
308  if( filever_major > 1 ){
309  if(output_result_global(fp)) {
310  return -1;
311  }
312  /* data header */
313  rc = fprintf(fp, "*data\n");
314  if(rc < 0) {
316  return -1;
317  }
318  }
319  if(output_result_dataheader(fp)) {
320  return -1;
321  }
322  if(output_result_node(fp)) {
323  return -1;
324  }
325  if(output_result_elem(fp)) {
326  return -1;
327  }
328 
329  return 0;
330 }
331 
332 /*---------------------------------------------------------------------------*/
333 
334 int HECMW_result_write_txt_by_fname(char *filename) {
335  FILE *fp = NULL;
336 
337  if (HECMW_ctrl_is_subdir()) {
338  if (HECMW_ctrl_make_subdir(filename)) {
339  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
340  HECMW_strmsg(errno));
341  goto error;
342  }
343  }
344 
345  if ((fp = fopen(filename, "w")) == NULL) {
346  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
347  HECMW_strmsg(errno));
348  goto error;
349  }
350 
351  if (output_result_data(fp)) {
352  goto error;
353  }
354 
355  if (fclose(fp)) {
357  goto error;
358  }
359  fp = NULL;
360 
361  return 0;
362 error:
363  if (fp) fclose(fp);
364  return -1;
365 }
366 
367 
368 /*---------------------------------------------------------------------------*/
369 /* TEXT MODE I/O ---- output_result_ST */
370 /*---------------------------------------------------------------------------*/
371 
372 
373 static int output_result_header_ST(struct hecmwST_result_data *result, char *header, FILE *fp) {
374  int rc,len;
375  char *p,*q;
376  char head[HECMW_HEADER_LEN+1];
377 
378  if(header == NULL) {
379  head[0] = '\0';
380  } else {
381  len = 0;
382  p = header;
383  q = head;
384  while(len < sizeof(head)-1 && *p && *p != '\n') {
385  *q++ = *p++;
386  len++;
387  }
388  *q++ = '\0';
389  }
390 
391  /* header */
392  if( filever_major > 1 ){
393  sprintf(head,"%s %d.%d",head,filever_major,filever_minor);
394  }
395  rc = fprintf(fp, "%s\n", head);
396  if(rc < 0) {
398  return -1;
399  }
400 
401  return 0;
402 }
403 
404 
405 static int output_result_global_ST(struct hecmwST_result_data *result, char *comment, FILE *fp) {
406  int i,j,k,n,rc,len;
407  char *p,*q;
408  char comment_line[HECMW_MSG_LEN+1];
409 
410  if(comment == NULL) {
411  comment_line[0] = '\0';
412  } else {
413  len = 0;
414  p = comment;
415  q = comment_line;
416  while(len < sizeof(comment_line)-1 && *p && *p != '\n') {
417  *q++ = *p++;
418  len++;
419  }
420  *q++ = '\0';
421  }
422 
423  /* comment */
424  rc = fprintf(fp, "*comment\n");
425  if(rc < 0) {
426  HECMW_set_error(HECMW_UTIL_E0205, "*comment");
427  return -1;
428  }
429  rc = fprintf(fp, "%s\n", comment);
430  if(rc < 0) {
431  HECMW_set_error(HECMW_UTIL_E0205, "comment");
432  return -1;
433  }
434 
435 
436  /* global header */
437  rc = fprintf(fp, "*global\n");
438  if(rc < 0) {
439  HECMW_set_error(HECMW_UTIL_E0205, "*global");
440  return -1;
441  }
442 
443  /* ng_component */
444  rc = fprintf(fp, "%d\n", result->ng_component);
445  if(rc < 0) {
446  HECMW_set_error(HECMW_UTIL_E0205, "ng_comp");
447  return -1;
448  }
449 
450  /* ng_dof */
451  n = 0;
452  for(i=0; i < result->ng_component; i++) {
453  rc = fprintf(fp, "%d%c", result->ng_dof[i], (n+1)%COL_INT ? ' ' : '\n');
454  if(rc < 0) {
456  return -1;
457  }
458  n++;
459  }
460  if(n % COL_INT) {
461  rc = fprintf(fp, "\n");
462  if(rc < 0) {
463  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
464  return -1;
465  }
466  }
467 
468  /* global_label */
469  for(i=0; i < result->ng_component; i++) {
470  rc = fprintf(fp, "%s\n", result->global_label[i]);
471  if(rc < 0) {
473  return -1;
474  }
475  }
476 
477  /* global_val_item */
478  if(result->ng_component == 0) return 0;
479  n = 0;
480  for(j=0; j < result->ng_component; j++) {
481  for(k=0; k < result->ng_dof[j]; k++) {
482  rc = fprintf(fp, "%.16E%c", result->global_val_item[n], (n+1)%COL_DOUBLE ? ' ' : '\n');
483  if(rc < 0) {
484  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
485  return -1;
486  }
487  n++;
488  }
489  }
490  if(n % COL_DOUBLE) {
491  rc = fprintf(fp, "\n");
492  if(rc < 0) {
494  return -1;
495  }
496  }
497 
498  /* dataheader */
499  rc = fprintf(fp, "*data\n");
500  if(rc < 0) {
502  return -1;
503  }
504 
505  return 0;
506 }
507 
508 
509 static int output_result_dataheader_ST(struct hecmwST_result_data *result,
510  int n_node, int n_elem, FILE *fp) {
511  int rc;
512 
513  /* n_node, n_elem */
514  rc = fprintf(fp, "%d %d\n", n_node, n_elem);
515  if(rc < 0) {
516  HECMW_set_error(HECMW_UTIL_E0205, "n_node,n_elem");
517  return -1;
518  }
519 
520  /* nn_component, ne_component */
521  rc = fprintf(fp, "%d %d\n", result->nn_component, result->ne_component);
522  if(rc < 0) {
523  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
524  return -1;
525  }
526 
527  return 0;
528 }
529 
530 
531 static int output_result_node_ST(struct hecmwST_result_data *result, int n_node, FILE *fp) {
532  int i,j,k,n,m,rc;
533 
534  /* nn_dof */
535  n = 0;
536  for(i=0; i < result->nn_component; i++) {
537  rc = fprintf(fp, "%d%c", result->nn_dof[i], (n+1)%COL_INT ? ' ' : '\n');
538  if(rc < 0) {
540  return -1;
541  }
542  n++;
543  }
544  if(n % COL_INT) {
545  rc = fprintf(fp, "\n");
546  if(rc < 0) {
547  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
548  return -1;
549  }
550  }
551 
552  /* node_label */
553  for(i=0; i < result->nn_component; i++) {
554  rc = fprintf(fp, "%s\n", result->node_label[i]);
555  if(rc < 0) {
557  return -1;
558  }
559  }
560 
561  /* node_val_item */
562  if(result->nn_component == 0) return 0;
563  m = 0;
564  for(i=0; i < n_node; i++) {
565  rc = fprintf(fp, "%d \n", node_global_ID[i]);
566  if(rc < 0) {
567  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
568  return -1;
569  }
570  n = 0;
571  for(j=0; j < result->nn_component; j++) {
572  for(k=0; k < result->nn_dof[j]; k++) {
573  rc = fprintf(fp, "%.16E%c", result->node_val_item[m], (n+1)%COL_DOUBLE ? ' ' : '\n');
574  if(rc < 0) {
575  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
576  return -1;
577  }
578  n++;
579  m++;
580  }
581  }
582  if(n % COL_DOUBLE) {
583  rc = fprintf(fp, "\n");
584  if(rc < 0) {
586  return -1;
587  }
588  }
589  }
590 
591  return 0;
592 }
593 
594 
595 static int output_result_elem_ST(struct hecmwST_result_data *result, int n_elem, FILE *fp) {
596  int i,j,k,n,m,rc;
597 
598  /* ne_dof */
599  n = 0;
600  for(i=0; i < result->ne_component; i++) {
601  rc = fprintf(fp, "%d%c", result->ne_dof[i], (n+1)%COL_INT ? ' ' : '\n');
602  if(rc < 0) {
604  return -1;
605  }
606  n++;
607  }
608  if(n % COL_INT) {
609  rc = fprintf(fp, "\n");
610  if(rc < 0) {
612  return -1;
613  }
614  }
615 
616  /* elem_label */
617  for(i=0; i < result->ne_component; i++) {
618  rc = fprintf(fp, "%s\n", result->elem_label[i]);
619  if(rc < 0) {
620  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
621  return -1;
622  }
623  }
624 
625  /* elem_val_item */
626  if(result->ne_component == 0) return 0;
627  m = 0;
628  for(i=0; i < n_elem; i++) {
629  rc = fprintf(fp, "%d\n", elem_global_ID[i]);
630  if(rc < 0) {
631  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
632  return -1;
633  }
634  n = 0;
635  for(j=0; j < result->ne_component; j++) {
636  for(k=0; k < result->ne_dof[j]; k++) {
637  rc = fprintf(fp, "%.16E%c", result->elem_val_item[m], (n+1)%COL_DOUBLE ? ' ' : '\n');
638  if(rc < 0) {
639  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
640  return -1;
641  }
642  n++;
643  m++;
644  }
645  }
646  if(n % COL_DOUBLE) {
647  rc = fprintf(fp, "\n");
648  if(rc < 0) {
650  return -1;
651  }
652  }
653  }
654 
655  return 0;
656 }
657 
658 
659 static int output_result_data_ST(struct hecmwST_result_data *result, int n_node, int n_elem,
660  char *header, char *comment, FILE *fp) {
661  HECMW_assert(fp);
662 
663  if(output_result_header_ST(result, header, fp)) {
664  return -1;
665  }
666  if( filever_major > 1 ){
667  if(output_result_global_ST(result, comment, fp)) {
668  return -1;
669  }
670  }
671  if(output_result_dataheader_ST(result, n_node, n_elem, fp)) {
672  return -1;
673  }
674  if(output_result_node_ST(result, n_node, fp)) {
675  return -1;
676  }
677  if(output_result_elem_ST(result, n_elem, fp)) {
678  return -1;
679  }
680 
681  return 0;
682 }
683 
684 /*---------------------------------------------------------------------------*/
685 
687  struct hecmwST_result_data *result,
688  int n_node, int n_elem, char *header, char *comment) {
689  FILE *fp = NULL;
690 
691  if (HECMW_ctrl_is_subdir()) {
692  if (HECMW_ctrl_make_subdir(filename)) {
693  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
694  HECMW_strmsg(errno));
695  goto error;
696  }
697  }
698 
699  if ((fp = fopen(filename, "w")) == NULL) {
700  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
701  HECMW_strmsg(errno));
702  goto error;
703  }
704 
705  if (output_result_data_ST(result, n_node, n_elem, header, comment, fp)) {
706  goto error;
707  }
708 
709  if (fclose(fp)) {
711  goto error;
712  }
713  fp = NULL;
714 
715  return 0;
716 error:
717  if (fp) fclose(fp);
718  return -1;
719 }
720 
721 
722 /*---------------------------------------------------------------------------*/
723 /* TEXT MODE I/O ---- input_result */
724 /*---------------------------------------------------------------------------*/
725 
726 
727 static int get_line(char *buf, int bufsize, FILE *fp) {
728  if(fgets(buf, bufsize, fp) == NULL) {
729  HECMW_set_error(HECMW_UTIL_E0205, "get_line");
730  return -1;
731  }
732  return strlen(line_buf);
733 }
734 
735 
736 static int input_result_header(struct hecmwST_result_data *result, FILE *fp) {
737  char *ptr;
738 
739  /* header */
740  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
741  return -1;
742  }
743  line_buf[ strlen(line_buf)-1 ] = 0;/* remove CR/LF*/
744  if( filever_major > 1 ){
745  ptr = strtok(line_buf, " ");
746  sprintf(line_buf, "%s", ptr);
747  }
748  strcpy( head, line_buf );
749 
750  return 0;
751 }
752 
753 
754 static int input_result_global(struct hecmwST_result_data *result, FILE *fp) {
755 #define DELIM " \n"
756  int i,rc,n;
757  char *p,*buf;
758 
759  /* comment */
760  if(get_line(line_buf, sizeof(line_buf), fp) < 0) { //skip comment header
761  return -1;
762  }
763  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
764  return -1;
765  }
766  line_buf[ strlen(line_buf)-1 ] = 0;/* remove CR/LF*/
767  strcpy( comment_line, line_buf );
768 
769 
770  /* skip global header */
771  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
772  return -1;
773  }
774 
775  /* ng_component */
776  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
777  return -1;
778  }
779  if(sscanf(line_buf, "%d", &result->ng_component) != 1) {
780  HECMW_set_error(HECMW_UTIL_E0205, "ng_comp");
781  return -1;
782  }
783 
784  if(result->ng_component <= 0) {
785  return 0;
786  }
787 
788  /* ng_dof */
789  result->ng_dof = HECMW_malloc(sizeof(*result->ng_dof)*result->ng_component);
790  if(result->ng_dof == NULL) {
791  HECMW_set_error(errno, "");
792  return -1;
793  }
794 
795  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
796  return -1;
797  }
798  i = n = 0;
799  buf = line_buf;
800  while(i < result->ng_component) {
801  p = strtok(buf, DELIM);
802  if(p == NULL) {
803  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
804  return -1;
805  }
806  buf = line_buf;
807  continue;
808  }
809  buf = NULL;
810  rc = sscanf(p, "%d", &result->ng_dof[i]);
811  if(rc == EOF) {
813  return -1;
814  }
815  if(rc != 1) {
817  return -1;
818  }
819  n += result->ng_dof[i];
820  i++;
821  }
822 
823  /* global_label */
824  result->global_label = HECMW_malloc(sizeof(*result->global_label)*result->ng_component);
825  if(result->global_label == NULL) {
826  HECMW_set_error(errno, "");
827  return -1;
828  }
829 
830  for(i=0; i < result->ng_component; i++) {
831  char label[HECMW_NAME_LEN+1];
832  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
833  return -1;
834  }
835  rc = sscanf(line_buf, "%s", label);
836  if(rc == EOF) {
838  return -1;
839  }
840  if(rc != 1) {
841  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
842  return -1;
843  }
844  result->global_label[i] = HECMW_strdup(label);
845  if(result->global_label[i] == NULL) {
846  HECMW_set_error(errno, "");
847  return -1;
848  }
849  }
850  /* global_val_item */
851  result->global_val_item = HECMW_malloc(sizeof(*result->global_val_item)*n);
852  if(result->global_val_item == NULL) {
853  HECMW_set_error(errno, "");
854  return -1;
855  }
856 
857  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
858  return -1;
859  }
860  i = 0;
861  buf = line_buf;
862  while(i < n) {
863  p = strtok(buf, DELIM);
864  if(p == NULL) {
865  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
866  return -1;
867  }
868  buf = line_buf;
869  continue;
870  }
871  buf = NULL;
872  rc = sscanf(p, "%lf", &result->global_val_item[i]);
873  if(rc == EOF) {
875  return -1;
876  }
877  if(rc != 1) {
878  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
879  return -1;
880  }
881  i++;
882  }
883 
884  /* skip data header */
885  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
886  return -1;
887  }
888 
889  return 0;
890 }
891 
892 
893 static int input_result_dataheader(struct hecmwST_result_data *result,
894  int *n_node, int *n_elem, FILE *fp) {
895 
896  /* n_node, n_elem */
897  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
898  return -1;
899  }
900  if(sscanf(line_buf, "%d%d", n_node, n_elem) != 2) {
901  HECMW_set_error(HECMW_UTIL_E0205, "n_node,n_elem");
902  return -1;
903  }
904 
905  /* nn_component, ne_component */
906  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
907  return -1;
908  }
909  if(sscanf(line_buf, "%d%d", &result->nn_component, &result->ne_component) != 2) {
910  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
911  return -1;
912  }
913 
914  return 0;
915 }
916 
917 
918 static int input_result_node(struct hecmwST_result_data *result, int n_node, FILE *fp) {
919 #define DELIM " \n"
920  int i,rc,n;
921  int label_counter;
922  char *p,*buf;
923 
924  if(result->nn_component <= 0) {
925  return 0;
926  }
927 
928  /* nn_dof */
929  result->nn_dof = HECMW_malloc(sizeof(*result->nn_dof)*result->nn_component);
930  if(result->nn_dof == NULL) {
931  HECMW_set_error(errno, "");
932  return -1;
933  }
934 
935  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
936  return -1;
937  }
938  i = n = 0;
939  buf = line_buf;
940  while(i < result->nn_component) {
941  p = strtok(buf, DELIM);
942  if(p == NULL) {
943  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
944  return -1;
945  }
946  buf = line_buf;
947  continue;
948  }
949  buf = NULL;
950  rc = sscanf(p, "%d", &result->nn_dof[i]);
951  if(rc == EOF) {
953  return -1;
954  }
955  if(rc != 1) {
957  return -1;
958  }
959  n += result->nn_dof[i];
960  i++;
961  }
962 
963  /* node_label */
964  result->node_label = HECMW_malloc(sizeof(*result->node_label)*result->nn_component);
965  if(result->node_label == NULL) {
966  HECMW_set_error(errno, "");
967  return -1;
968  }
969 
970  for(i=0; i < result->nn_component; i++) {
971  char label[HECMW_NAME_LEN+1];
972  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
973  return -1;
974  }
975  rc = sscanf(line_buf, "%s", label);
976  if(rc == EOF) {
978  return -1;
979  }
980  if(rc != 1) {
981  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
982  return -1;
983  }
984  result->node_label[i] = HECMW_strdup(label);
985  if(result->node_label[i] == NULL) {
986  HECMW_set_error(errno, "");
987  return -1;
988  }
989  }
990  /* node_val_item */
991  node_global_ID = HECMW_malloc(sizeof(*node_global_ID)*n_node);
992  if(node_global_ID == NULL) {
993  HECMW_set_error(errno, "");
994  return -1;
995  }
996  result->node_val_item = HECMW_malloc(sizeof(*result->node_val_item)*n*n_node);
997  if(result->node_val_item == NULL) {
998  HECMW_set_error(errno, "");
999  return -1;
1000  }
1001 
1002  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1003  return -1;
1004  }
1005  i = 0;
1006  label_counter = 0;
1007  n++; /**** For global node ID ****/
1008  buf = line_buf;
1009  while(i < n*n_node) {
1010  p = strtok(buf, DELIM);
1011  if(p == NULL) {
1012  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1013  return -1;
1014  }
1015  buf = line_buf;
1016  continue;
1017  }
1018  buf = NULL;
1019  if ( (i%n) == 0 ) {
1020  rc = sscanf(p, "%d", &node_global_ID[label_counter]);
1021  label_counter++;
1022  } else {
1023  rc = sscanf(p, "%lf", &result->node_val_item[i-label_counter]);
1024  }
1025  if(rc == EOF) {
1027  return -1;
1028  }
1029  if(rc != 1) {
1030  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
1031  return -1;
1032  }
1033  i++;
1034  }
1035 
1036  return 0;
1037 }
1038 
1039 
1040 static int input_result_elem(struct hecmwST_result_data *result, int n_elem, FILE *fp) {
1041 #define DELIM " \n"
1042  int i,rc,n;
1043  int label_counter;
1044  char *p,*buf;
1045 
1046  if(result->ne_component <= 0) {
1047  return 0;
1048  }
1049 
1050  /* ne_dof */
1051  result->ne_dof = HECMW_malloc(sizeof(*result->ne_dof)*result->ne_component);
1052  if(result->ne_dof == NULL) {
1053  HECMW_set_error(errno, "");
1054  return -1;
1055  }
1056 
1057  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1058  return -1;
1059  }
1060  i = n = 0;
1061  buf = line_buf;
1062  while(i < result->ne_component) {
1063 
1064  p = strtok(buf, DELIM);
1065  if(p == NULL) {
1066  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1067  return -1;
1068  }
1069  buf = line_buf;
1070  continue;
1071  }
1072  buf = NULL;
1073  rc = sscanf(p, "%d", &result->ne_dof[i]);
1074  if(rc == EOF) {
1076  return -1;
1077  }
1078  if(rc != 1) {
1079  HECMW_set_error(HECMW_UTIL_E0205, "ne_dof");
1080  return -1;
1081  }
1082  n += result->ne_dof[i];
1083  i++;
1084  }
1085 
1086  /* elem_label */
1087  result->elem_label = HECMW_malloc(sizeof(*result->elem_label)*result->ne_component);
1088  if(result->elem_label == NULL) {
1089  HECMW_set_error(errno, "");
1090  return -1;
1091  }
1092 
1093  for(i=0; i < result->ne_component; i++) {
1094  char label[HECMW_NAME_LEN+1];
1095  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1096  return -1;
1097  }
1098  rc = sscanf(line_buf, "%s", label);
1099  if(rc == EOF) {
1101  return -1;
1102  }
1103  if(rc != 1) {
1104  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
1105  return -1;
1106  }
1107  result->elem_label[i] = HECMW_strdup(label);
1108  if(result->elem_label[i] == NULL) {
1109  HECMW_set_error(errno, "");
1110  return -1;
1111  }
1112  }
1113 
1114  /* elem_val_item */
1115  elem_global_ID = HECMW_malloc(sizeof(*elem_global_ID)*n_elem);
1116  if(elem_global_ID == NULL) {
1117  HECMW_set_error(errno, "");
1118  return -1;
1119  }
1120  result->elem_val_item = HECMW_malloc(sizeof(*result->elem_val_item)*n*n_elem);
1121  if(result->elem_val_item == NULL) {
1122  HECMW_set_error(errno, "");
1123  return -1;
1124  }
1125 
1126  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1127  return -1;
1128  }
1129  i = 0;
1130  label_counter = 0;
1131  n++; /**** For global element ID ****/
1132  buf = line_buf;
1133  while(i < n*n_elem) {
1134  p = strtok(buf, DELIM);
1135  if(p == NULL) {
1136  if(get_line(line_buf, sizeof(line_buf), fp) < 0) {
1137  return -1;
1138  }
1139  buf = line_buf;
1140  continue;
1141  }
1142  buf = NULL;
1143  if ( (i%n) == 0 ) {
1144  rc = sscanf(p, "%d", &elem_global_ID[label_counter]);
1145  label_counter++;
1146  } else {
1147  rc = sscanf(p, "%lf", &result->elem_val_item[i-label_counter]);
1148  }
1149  if(rc == EOF) {
1151  return -1;
1152  }
1153  if(rc != 1) {
1154  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
1155  return -1;
1156  }
1157  i++;
1158  }
1159 
1160  return 0;
1161 }
1162 
1163 
1164 static struct hecmwST_result_data *input_result_data(FILE *fp) {
1165  int n_node, n_elem;
1166  struct hecmwST_result_data *result;
1167 
1168  HECMW_assert(fp);
1169 
1170  result = HECMW_calloc(1, sizeof(*result));
1171  if(result == NULL) {
1172  HECMW_set_error(errno, "");
1173  return NULL;
1174  }
1175  if(input_result_header(result, fp)) {
1176  return NULL;
1177  }
1178  if( filever_major > 1 ){
1179  if(input_result_global(result, fp)) {
1180  return NULL;
1181  }
1182  }
1183  if(input_result_dataheader(result, &n_node, &n_elem, fp)) {
1184  return NULL;
1185  }
1186  nnode = n_node;
1187  nelem = n_elem;
1188  if(input_result_node(result, n_node, fp)) {
1189  return NULL;
1190  }
1191  if(input_result_elem(result, n_elem, fp)) {
1192  return NULL;
1193  }
1194 
1195  return result;
1196 }
1197 
1198 /*---------------------------------------------------------------------------*/
1199 
1201  FILE *fp;
1202  struct hecmwST_result_data *result;
1203 
1204  if((fp = fopen(filename, "r")) == NULL) {
1205  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename, HECMW_strmsg(errno));
1206  return NULL;
1207  }
1208 
1209  result = input_result_data(fp);
1210  if(result == NULL) {
1211  return NULL;
1212  }
1213 
1214  if(fclose(fp)) {
1216  return NULL;
1217  }
1218 
1219  return result;
1220 }
#define HECMW_MSG_LEN
Definition: hecmw_config.h:74
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:68
#define HECMW_NAME_LEN
Definition: hecmw_config.h:70
int HECMW_ctrl_is_subdir(void)
int HECMW_ctrl_make_subdir(char *filename)
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
#define NULL
#define HECMW_calloc(nmemb, size)
Definition: hecmw_malloc.h:21
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
#define HECMW_strdup(s)
Definition: hecmw_malloc.h:23
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
char * HECMW_strmsg(int msgno)
Definition: hecmw_msg.c:31
#define HECMW_UTIL_E0204
Definition: hecmw_msgno.h:366
#define HECMW_UTIL_E0205
Definition: hecmw_msgno.h:367
#define HECMW_UTIL_E0201
Definition: hecmw_msgno.h:363
#define HECMW_UTIL_E0202
Definition: hecmw_msgno.h:364
struct result_list * node_list
int filever_major
struct result_list * elem_list
char line_buf[LINEBUF_SIZE+1]
int * node_global_ID
int filever_minor
int HECMW_result_count_ne_comp(void)
int HECMW_result_count_ng_comp(void)
struct result_list * global_list
int * elem_global_ID
int nelem
int nnode
char comment_line[HECMW_MSG_LEN+1]
int HECMW_result_count_nn_comp(void)
char head[HECMW_HEADER_LEN+1]
#define COL_DOUBLE
#define COL_INT
#define DELIM
struct hecmwST_result_data * HECMW_result_read_txt_by_fname(char *filename)
int HECMW_result_write_txt_by_fname(char *filename)
int HECMW_result_write_txt_ST_by_fname(char *filename, struct hecmwST_result_data *result, int n_node, int n_elem, char *header, char *comment)
#define HECMW_assert(cond)
Definition: hecmw_util.h:40
CNFData data
double * elem_val_item
Definition: hecmw_result.h:23
double * global_val_item
Definition: hecmw_result.h:21
double * node_val_item
Definition: hecmw_result.h:22
double * ptr
struct result_list * next