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