FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_result_io_bin.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_bin_io.h"
12 #include "hecmw_result.h"
13 #include "hecmw_result_io.h"
14 
15 #define RES_BIN_HEADER "HECMW_BINARY_RESULT"
16 
17 #define LINEBUF_SIZE 1023
18 static char Line_Buf[LINEBUF_SIZE + 1];
19 
20 
21 /*---------------------------------------------------------------------------*/
22 /* BINARY MODE I/O --- bin_header */
23 /*---------------------------------------------------------------------------*/
24 
25 
26 static int write_bin_header(FILE* fp) {
27  char* s = (char*)RES_BIN_HEADER;
28  size_t n;
29  char nbyte[3];
30 
31  n = strlen(s);
32  if( fwrite( s, sizeof(char), n, fp) != n ) return -1;
33  n = sizeof(long);
34  snprintf( nbyte, sizeof(nbyte), "%2zd", n );
35  if( fwrite( nbyte, sizeof(char), 2, fp) != 2 ) return -1;
36  return 0;
37 }
38 
39 
40 static int check_bin_header(FILE* fp) {
41  char* s = (char*)RES_BIN_HEADER;
42  size_t n = strlen(s);
43  char buff[256], nbyte[3];
44 
45  if( fread( buff, sizeof(char), n, fp) != n ) return 0;
46  if( fread( nbyte, sizeof(char), 2, fp) != 2 ) return 0;
47 
48  buff[n] = 0;
49  return ( strcmp( buff, s ) == 0 );
50 }
51 
52 
53 int HECMW_result_io_bin_judge_file(char *filename) {
54  int rcode;
55  FILE* fp;
56 
57  if((fp = fopen(filename, "rb")) == NULL) {
58  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename, HECMW_strmsg(errno));
59  return 0;
60  }
61 
63  rcode = check_bin_header(fp);
64  fclose(fp);
65 
66  return rcode;
67 }
68 
69 
70 /*---------------------------------------------------------------------------*/
71 /* BINARY MODE I/O --- bin_output_result */
72 /*---------------------------------------------------------------------------*/
73 
74 
75 static int bin_output_result_header(FILE *fp) {
76  int rc;
77 
78  /* header */
80  char temp_head[HECMW_HEADER_LEN + 16];
81  snprintf(temp_head, sizeof(temp_head), "%s %d.%d", ResIO.head, HECMW_RESULT_FILEVER_MAJOR, HECMW_RESULT_FILEVER_MINOR);
82  snprintf(ResIO.head, sizeof(ResIO.head), "%s", temp_head);
83  }
84  rc = hecmw_write_bin(fp,"S", ResIO.head);
85  if(rc < 0) {
87  return -1;
88  }
89 
90  return 0;
91 }
92 
93 
94 static int bin_output_result_global(FILE *fp) {
95  int i,j,k,n,rc,ng_comp;
96  struct result_list *p,**data;
97 
98  /* comment */
99  rc = hecmw_write_bin(fp,"S","*comment");
100  if(rc < 0) {
101  HECMW_set_error(HECMW_UTIL_E0205, "*comment");
102  return -1;
103  }
104  rc = hecmw_write_bin(fp,"S", ResIO.comment_line);
105  if(rc < 0) {
107  return -1;
108  }
109 
110  /* global header */
111  rc = hecmw_write_bin(fp,"S","*global");
112  if(rc < 0) {
113  HECMW_set_error(HECMW_UTIL_E0205, "*global");
114  return -1;
115  }
116 
117  /* ng_component */
119  if(rc < 0) {
120  HECMW_set_error(HECMW_UTIL_E0205, "ng_comp");
121  return -1;
122  }
123 
124  /* ng_dof */
125  n = 0;
126  for(p=ResIO.global_list; p; p=p->next) {
127  rc = hecmw_write_bin(fp, "I", p->n_dof );
128  if(rc < 0) {
130  return -1;
131  }
132  n++;
133  }
134 
135  /* global_label */
136  for(p=ResIO.global_list; p; p=p->next) {
137  rc = hecmw_write_bin(fp, "S", p->label);
138  if(rc < 0) {
139  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
140  return -1;
141  }
142  }
143 
144  /* global_val_item */
145  ng_comp = HECMW_result_io_count_ng_comp();
146  if(ng_comp == 0) return 0;
147  data = HECMW_malloc(sizeof(*data) * ng_comp);
148  if(data == NULL) {
149  HECMW_set_error(errno, "");
150  return -1;
151  }
152  i = 0;
153  for(p=ResIO.global_list; p; p=p->next) {
154  data[i++] = p;
155  }
156  for(j=0; j < ng_comp; j++) {
157  p = data[j];
158  for(k=0; k < p->n_dof; k++) {
159  rc = hecmw_write_bin(fp, "F", p->ptr[k] );
160  if(rc < 0) {
161  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
162  return -1;
163  }
164  }
165  }
166  HECMW_free(data);
167 
168  return 0;
169 }
170 
171 
172 static int bin_output_result_dataheader(FILE *fp) {
173  int rc;
174 
175  /* n_node, n_elem */
176  rc = hecmw_write_bin(fp, "II", ResIO.nnode, ResIO.nelem);
177  if(rc < 0) {
178  HECMW_set_error(HECMW_UTIL_E0205, "nnode,nelem");
179  return -1;
180  }
181 
182  /* nn_component, ne_component */
184  if(rc < 0) {
185  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
186  return -1;
187  }
188 
189  return 0;
190 }
191 
192 
193 static int bin_output_result_node(FILE *fp) {
194  int i,j,k,n,rc,nn_comp;
195  struct result_list *p,**data;
196 
197  /* nn_dof */
198  n = 0;
199  for(p=ResIO.node_list; p; p=p->next) {
200  rc = hecmw_write_bin(fp, "I", p->n_dof );
201  if(rc < 0) {
203  return -1;
204  }
205  n++;
206  }
207 
208  /* node_label */
209  for(p=ResIO.node_list; p; p=p->next) {
210  rc = hecmw_write_bin(fp, "S", p->label);
211  if(rc < 0) {
212  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
213  return -1;
214  }
215  }
216 
217  /* node_val_item */
218  nn_comp = HECMW_result_io_count_nn_comp();
219  if(nn_comp == 0) return 0;
220  data = HECMW_malloc(sizeof(*data) * nn_comp);
221  if(data == NULL) {
222  HECMW_set_error(errno, "");
223  return -1;
224  }
225  i = 0;
226  for(p=ResIO.node_list; p; p=p->next) {
227  data[i++] = p;
228  }
229  for(i=0; i < ResIO.nnode; i++) {
230  rc = hecmw_write_bin(fp, "I", ResIO.node_global_ID[i] );
231  if(rc < 0) {
232  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
233  return -1;
234  }
235  for(j=0; j < nn_comp; j++) {
236  p = data[j];
237  for(k=0; k < p->n_dof; k++) {
238  rc = hecmw_write_bin(fp, "F", p->ptr[i*p->n_dof+k] );
239  if(rc < 0) {
240  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
241  return -1;
242  }
243  }
244  }
245  }
246  HECMW_free(data);
247 
248  return 0;
249 }
250 
251 
252 static int bin_output_result_elem(FILE *fp) {
253  int i,j,k,n,rc,ne_comp;
254  struct result_list *p,**data;
255 
256  /* ne_dof */
257  n = 0;
258  for(p=ResIO.elem_list; p; p=p->next) {
259  rc = hecmw_write_bin(fp, "I", p->n_dof );
260  if(rc < 0) {
262  return -1;
263  }
264  n++;
265  }
266 
267  /* elem_label */
268  for(p=ResIO.elem_list; p; p=p->next) {
269  rc = hecmw_write_bin(fp, "S", p->label);
270  if(rc < 0) {
271  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
272  return -1;
273  }
274  }
275 
276  /* elem_val_item */
277  ne_comp = HECMW_result_io_count_ne_comp();
278  if(ne_comp == 0) return 0;
279  data = HECMW_malloc(sizeof(*data) * ne_comp);
280  if(data == NULL) {
281  HECMW_set_error(errno, "");
282  return -1;
283  }
284  i = 0;
285  for(p=ResIO.elem_list; p; p=p->next) {
286  data[i++] = p;
287  }
288  for(i=0; i < ResIO.nelem; i++) {
289  rc = hecmw_write_bin(fp, "I", ResIO.elem_global_ID[i] );
290  if(rc < 0) {
291  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
292  return -1;
293  }
294  for(j=0; j < ne_comp; j++) {
295  p = data[j];
296  for(k=0; k < p->n_dof; k++) {
297  rc = hecmw_write_bin(fp, "F", p->ptr[i*p->n_dof+k] );
298  if(rc < 0) {
299  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
300  return -1;
301  }
302  }
303  }
304  }
305  HECMW_free(data);
306 
307  return 0;
308 }
309 
310 
311 static int bin_output_result_data(FILE *fp) {
312  int rc;
313  HECMW_assert(fp);
314 
315  if(bin_output_result_header(fp)) {
316  return -1;
317  }
318  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
319  if(bin_output_result_global(fp)) {
320  return -1;
321  }
322  /* data header */
323  rc = hecmw_write_bin(fp,"S","*data");
324  if(rc < 0) {
326  return -1;
327  }
328  }
329  if(bin_output_result_dataheader(fp)) {
330  return -1;
331  }
332  if(bin_output_result_node(fp)) {
333  return -1;
334  }
335  if(bin_output_result_elem(fp)) {
336  return -1;
337  }
338 
339  return 0;
340 }
341 
342 /*---------------------------------------------------------------------------*/
343 
345  FILE *fp = NULL;
346 
347  if (HECMW_ctrl_is_subdir()) {
348  if (HECMW_ctrl_make_subdir(filename)) {
349  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
350  HECMW_strmsg(errno));
351  goto error;
352  }
353  }
354 
355  if ((fp = fopen(filename, "wb")) == NULL) {
356  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
357  HECMW_strmsg(errno));
358  goto error;
359  }
360 
362  if (write_bin_header(fp)) goto error;
363  if (bin_output_result_data(fp)) goto error;
364 
365  if (fclose(fp)) {
367  goto error;
368  }
369  fp = NULL;
370 
371  return 0;
372 error:
373  if (fp) fclose(fp);
374  return -1;
375 }
376 
377 
378 /*---------------------------------------------------------------------------*/
379 /* BINARY MODE I/O --- bin_output_result_ST */
380 /*---------------------------------------------------------------------------*/
381 
382 
383 static int bin_output_result_header_ST(struct hecmwST_result_data *result,
384  char *header, FILE *fp) {
385  size_t len;
386  int rc;
387  char *p,*q;
388  char head[HECMW_HEADER_LEN+1];
389 
390  if(header == NULL) {
391  head[0] = '\0';
392  } else {
393  len = 0;
394  p = header;
395  q = head;
396  while(len < sizeof(head)-1 && *p && *p != '\n') {
397  *q++ = *p++;
398  len++;
399  }
400  *q++ = '\0';
401  }
402 
403  /* header */
404  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
405  char tmp[HECMW_HEADER_LEN+1];
406  snprintf(tmp, sizeof(tmp), "%s %d.%d", head, HECMW_RESULT_FILEVER_MAJOR, HECMW_RESULT_FILEVER_MINOR);
407  memcpy(head, tmp, sizeof(head));
408  }
409  rc = hecmw_write_bin(fp, "S", header);
410  if(rc < 0) {
412  return -1;
413  }
414 
415  return 0;
416 }
417 
418 
419 static int bin_output_result_global_ST(struct hecmwST_result_data *result,
420  char *comment, FILE *fp) {
421  size_t len;
422  int i,j,k,n,m,rc;
423  char *p,*q;
424  char comment_line[HECMW_MSG_LEN+1];
425 
426  if(comment == NULL) {
427  comment_line[0] = '\0';
428  } else {
429  len = 0;
430  p = comment;
431  q = comment_line;
432  while(len < sizeof(comment_line)-1 && *p && *p != '\n') {
433  *q++ = *p++;
434  len++;
435  }
436  *q++ = '\0';
437  }
438 
439  /* comment */
440  rc = hecmw_write_bin(fp,"S","*comment");
441  if(rc < 0) {
442  HECMW_set_error(HECMW_UTIL_E0205, "*comment");
443  return -1;
444  }
445  rc = hecmw_write_bin(fp, "S", comment);
446  if(rc < 0) {
447  HECMW_set_error(HECMW_UTIL_E0205, "comment");
448  return -1;
449  }
450 
451  /* global header */
452  rc = hecmw_write_bin(fp,"S","*global");
453  if(rc < 0) {
454  HECMW_set_error(HECMW_UTIL_E0205, "*global");
455  return -1;
456  }
457 
458  /* ng_component */
459  rc = hecmw_write_bin(fp, "II", result->ng_component);
460  if(rc < 0) {
461  HECMW_set_error(HECMW_UTIL_E0205, "ng_comp");
462  return -1;
463  }
464 
465  /* ng_dof */
466  n = 0;
467  for(i=0; i < result->ng_component; i++) {
468  rc = hecmw_write_bin(fp, "I", result->ng_dof[i] );
469  if(rc < 0) {
471  return -1;
472  }
473  n++;
474  }
475 
476  /* global_label */
477  for(i=0; i < result->ng_component; i++) {
478  rc = hecmw_write_bin(fp, "S", result->global_label[i]);
479  if(rc < 0) {
480  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
481  return -1;
482  }
483  }
484 
485  /* global_val_item */
486  if(result->ng_component == 0) return 0;
487  m = 0;
488  for(j=0; j < result->ng_component; j++) {
489  for(k=0; k < result->ng_dof[j]; k++) {
490  rc = hecmw_write_bin(fp, "F", result->global_val_item[m] );
491  if(rc < 0) {
492  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
493  return -1;
494  }
495  m++;
496  }
497  }
498 
499  /* data header */
500  rc = hecmw_write_bin(fp,"S","*data");
501  if(rc < 0) {
503  return -1;
504  }
505 
506  return 0;
507 }
508 
509 
510 static int bin_output_result_dataheader_ST(struct hecmwST_result_data *result,
511  int n_node, int n_elem, FILE *fp) {
512  int rc;
513 
514  /* n_node, n_elem */
515  rc = hecmw_write_bin(fp, "II", n_node, n_elem);
516  if(rc < 0) {
517  HECMW_set_error(HECMW_UTIL_E0205, "n_node,n_elem");
518  return -1;
519  }
520 
521  /* nn_component, ne_component */
522  rc = hecmw_write_bin(fp, "II", result->nn_component, result->ne_component);
523  if(rc < 0) {
524  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
525  return -1;
526  }
527 
528  return 0;
529 }
530 
531 
532 static int bin_output_result_node_ST(struct hecmwST_result_data *result,
533  int n_node, FILE *fp) {
534  int i,j,k,n,m,rc;
535 
536  /* nn_dof */
537  n = 0;
538  for(i=0; i < result->nn_component; i++) {
539  rc = hecmw_write_bin(fp, "I", result->nn_dof[i] );
540  if(rc < 0) {
542  return -1;
543  }
544  n++;
545  }
546 
547  /* node_label */
548  for(i=0; i < result->nn_component; i++) {
549  rc = hecmw_write_bin(fp, "S", result->node_label[i]);
550  if(rc < 0) {
551  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
552  return -1;
553  }
554  }
555 
556  /* node_val_item */
557  if(result->nn_component == 0) return 0;
558  m = 0;
559  for(i=0; i < n_node; i++) {
560  rc = hecmw_write_bin(fp, "I", ResIO.node_global_ID[i] );
561  if(rc < 0) {
562  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
563  return -1;
564  }
565  for(j=0; j < result->nn_component; j++) {
566  for(k=0; k < result->nn_dof[j]; k++) {
567  rc = hecmw_write_bin(fp, "F", result->node_val_item[m] );
568  if(rc < 0) {
569  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
570  return -1;
571  }
572  m++;
573  }
574  }
575  }
576 
577  return 0;
578 }
579 
580 
581 static int bin_output_result_elem_ST(struct hecmwST_result_data *result,
582  int n_elem, FILE *fp) {
583  int i,j,k,n,m,rc;
584 
585  /* ne_dof */
586  n = 0;
587  for(i=0; i < result->ne_component; i++) {
588  rc = hecmw_write_bin(fp, "I", result->ne_dof[i] );
589  if(rc < 0) {
591  return -1;
592  }
593  n++;
594  }
595 
596  /* elem_label */
597  for(i=0; i < result->ne_component; i++) {
598  rc = hecmw_write_bin(fp, "S", result->elem_label[i]);
599  if(rc < 0) {
600  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
601  return -1;
602  }
603  }
604 
605  /* elem_val_item */
606  if(result->ne_component == 0) return 0;
607  m = 0;
608  for(i=0; i < n_elem; i++) {
609  rc = hecmw_write_bin(fp, "I", ResIO.elem_global_ID[i] );
610  if(rc < 0) {
611  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
612  return -1;
613  }
614  for(j=0; j < result->ne_component; j++) {
615  for(k=0; k < result->ne_dof[j]; k++) {
616  rc = hecmw_write_bin(fp, "F", result->elem_val_item[m]);
617  if(rc < 0) {
618  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
619  return -1;
620  }
621  m++;
622  }
623  }
624  }
625 
626  return 0;
627 }
628 
629 
630 static int bin_output_result_data_ST(struct hecmwST_result_data *result,
631  int n_node, int n_elem, char *header,
632  char *comment, FILE *fp) {
633  HECMW_assert(fp);
634 
635  if(bin_output_result_header_ST(result, header, fp)) {
636  return -1;
637  }
638  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
639  if(bin_output_result_global_ST(result, comment, fp)) {
640  return -1;
641  }
642  }
643  if(bin_output_result_dataheader_ST(result, n_node, n_elem, fp)) {
644  return -1;
645  }
646  if(bin_output_result_node_ST(result, n_node, fp)) {
647  return -1;
648  }
649  if(bin_output_result_elem_ST(result, n_elem, fp)) {
650  return -1;
651  }
652 
653  return 0;
654 }
655 
656 /*---------------------------------------------------------------------------*/
657 
659  struct hecmwST_result_data *result,
660  int n_node, int n_elem, char *header, char *comment) {
661  FILE *fp = NULL;
662 
663  if (HECMW_ctrl_is_subdir()) {
664  if (HECMW_ctrl_make_subdir(filename)) {
665  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
666  HECMW_strmsg(errno));
667  goto error;
668  }
669  }
670 
671  if ((fp = fopen(filename, "wb")) == NULL) {
672  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
673  HECMW_strmsg(errno));
674  goto error;
675  }
676 
678  if (write_bin_header(fp)) goto error;
679  if (bin_output_result_data_ST(result, n_node, n_elem, header, comment, fp)) goto error;
680 
681  if (fclose(fp)) {
683  goto error;
684  }
685  fp = NULL;
686 
687  return 0;
688 error:
689  if (fp) fclose(fp);
690  return -1;
691 }
692 
693 
694 /*---------------------------------------------------------------------------*/
695 /* BINARY MODE I/O --- bin_input_result */
696 /*---------------------------------------------------------------------------*/
697 
698 
699 static int bin_input_result_header(struct hecmwST_result_data *result, FILE *fp) {
700  char *ptr;
701 
702  /* header */
703  if(hecmw_read_bin(fp, "S", Line_Buf)) {
705  return -1;
706  }
707  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
708  ptr = strtok(Line_Buf, " ");
709  if(ptr != Line_Buf) {
710  size_t len = strlen(ptr);
711  memmove(Line_Buf, ptr, len + 1);
712  } else {
713  ptr[strlen(ptr)] = '\0';
714  }
715  }
716  snprintf(ResIO.head, sizeof(ResIO.head), "%s", Line_Buf);
717 
718  return 0;
719 }
720 
721 
722 static int bin_input_result_global(struct hecmwST_result_data *result, FILE *fp) {
723  int i,j,k,n,m;
724  /* comment */
725  if(hecmw_read_bin(fp, "S", Line_Buf)) { //skip comment header
726  HECMW_set_error(HECMW_UTIL_E0205, "comment");
727  return -1;
728  }
729  if(hecmw_read_bin(fp, "S", Line_Buf)) {
730  HECMW_set_error(HECMW_UTIL_E0205, "comment");
731  return -1;
732  }
733  snprintf(ResIO.comment_line, sizeof(ResIO.comment_line), "%s", Line_Buf);
734 
735  /* skip global header */
736  if(hecmw_read_bin(fp, "S", Line_Buf)) {
737  HECMW_set_error(HECMW_UTIL_E0205, "*global");
738  return -1;
739  }
740 
741  /* ng_component */
742  if(hecmw_read_bin(fp, "II", &result->ng_component)) {
743  HECMW_set_error(HECMW_UTIL_E0205, "ng_component");
744  return -1;
745  }
746 
747  if(result->ng_component <= 0) {
748  return 0;
749  }
750 
751  /* ng_dof */
752  result->ng_dof = HECMW_malloc(sizeof(*result->ng_dof)*result->ng_component);
753  if(result->ng_dof == NULL) {
754  HECMW_set_error(errno, "");
755  return -1;
756  }
757 
758  n = 0;
759  for(i=0; i<result->ng_component; i++){
760  if(hecmw_read_bin( fp, "I", &result->ng_dof[i] )) {
762  return -1;
763  }
764  n += result->ng_dof[i];
765  }
766 
767  /* global_label */
768  result->global_label = HECMW_malloc(sizeof(*result->global_label)*result->ng_component);
769  if(result->global_label == NULL) {
770  HECMW_set_error(errno, "(global_label)");
771  return -1;
772  }
773 
774  for(i=0; i < result->ng_component; i++) {
775  char label[HECMW_NAME_LEN+1];
776  if(hecmw_read_bin( fp, "S", label )) {
777  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
778  return -1;
779  }
780  result->global_label[i] = HECMW_strdup(label);
781  if(result->global_label[i] == NULL) {
782  HECMW_set_error(errno, "(label)");
783  return -1;
784  }
785  }
786 
787  /* global_val_item */
788  result->global_val_item = HECMW_malloc(sizeof(*result->global_val_item)*n);
789  if(result->global_val_item == NULL) {
790  HECMW_set_error(errno, "(global_val_item)");
791  return -1;
792  }
793 
794  m = 0;
795  for(j=0; j < result->ng_component; j++) {
796  for(k=0; k < result->ng_dof[j]; k++) {
797  if(hecmw_read_bin( fp, "F", &result->global_val_item[m] )) {
798  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
799  return -1;
800  }
801  m++;
802  }
803  }
804 
805  /* skip data header */
806  if(hecmw_read_bin(fp, "S", Line_Buf)) {
808  return -1;
809  }
810 
811  return 0;
812 }
813 
814 
815 static int bin_input_result_dataheader(struct hecmwST_result_data *result,
816  int *n_node, int *n_elem, FILE *fp) {
817  int nn, ne;
818 
819  /* n_node, n_elem */
820  if(hecmw_read_bin(fp, "II", &nn, &ne)) {
821  HECMW_set_error(HECMW_UTIL_E0205, "n_node,n_elem");
822  return -1;
823  }
824  *n_node = nn;
825  *n_elem = ne;
826 
827  /* nn_component, ne_component */
828  if(hecmw_read_bin(fp, "II", &result->nn_component, &result->ne_component)) {
829  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
830  return -1;
831  }
832 
833  return 0;
834 }
835 
836 
837 static int bin_input_result_node(struct hecmwST_result_data *result, int n_node, FILE *fp) {
838  int i,j,k,n,m;
839 
840  if(result->nn_component <= 0) {
841  return 0;
842  }
843 
844  /* nn_dof */
845  result->nn_dof = HECMW_malloc(sizeof(*result->nn_dof)*result->nn_component);
846  if(result->nn_dof == NULL) {
847  HECMW_set_error(errno, "");
848  return -1;
849  }
850 
851  n = 0;
852  for(i=0; i<result->nn_component; i++){
853  if(hecmw_read_bin( fp, "I", &result->nn_dof[i] )) {
855  return -1;
856  }
857  n += result->nn_dof[i];
858  }
859 
860  /* node_label */
861  result->node_label = HECMW_malloc(sizeof(*result->node_label)*result->nn_component);
862  if(result->node_label == NULL) {
863  HECMW_set_error(errno, "(node_label)");
864  return -1;
865  }
866 
867  for(i=0; i < result->nn_component; i++) {
868  char label[HECMW_NAME_LEN+1];
869  if(hecmw_read_bin( fp, "S", label )) {
870  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
871  return -1;
872  }
873  result->node_label[i] = HECMW_strdup(label);
874  if(result->node_label[i] == NULL) {
875  HECMW_set_error(errno, "(label)");
876  return -1;
877  }
878  }
879 
880  /* node_val_item */
882  if(ResIO.node_global_ID == NULL) {
883  HECMW_set_error(errno, "(node_global_ID)");
884  return -1;
885  }
886  result->node_val_item = HECMW_malloc(sizeof(*result->node_val_item)*n*n_node);
887  if(result->node_val_item == NULL) {
888  HECMW_set_error(errno, "(node_val_item)");
889  return -1;
890  }
891 
892  m = 0;
893  for(i=0; i < n_node; i++) {
894  if(hecmw_read_bin( fp, "I", &ResIO.node_global_ID[i] )) {
895  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
896  return -1;
897  }
898  for(j=0; j < result->nn_component; j++) {
899  for(k=0; k < result->nn_dof[j]; k++) {
900  if(hecmw_read_bin( fp, "F", &result->node_val_item[m] )) {
901  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
902  return -1;
903  }
904  m++;
905  }
906  }
907 
908  }
909 
910  return 0;
911 }
912 
913 
914 static int bin_input_result_elem(struct hecmwST_result_data *result, int n_elem, FILE *fp) {
915  int i,j,k,n,m;
916 
917  if(result->ne_component <= 0) {
918  return 0;
919  }
920 
921  /* ne_dof */
922  result->ne_dof = HECMW_malloc(sizeof(*result->ne_dof)*result->ne_component);
923  if(result->ne_dof == NULL) {
924  HECMW_set_error(errno, "(ne_dof)");
925  return -1;
926  }
927 
928  n = 0;
929  for(i=0; i<result->ne_component;i++ ){
930  if(hecmw_read_bin( fp, "I", &result->ne_dof[i] )) {
932  return -1;
933  }
934  n += result->ne_dof[i];
935  }
936 
937  /* elem_label */
938  result->elem_label = HECMW_malloc(sizeof(*result->elem_label)*result->ne_component);
939  if(result->elem_label == NULL) {
940  HECMW_set_error(errno, "(elem_label)");
941  return -1;
942  }
943 
944  for(i=0; i < result->ne_component; i++) {
945  char label[HECMW_NAME_LEN+1];
946  if(hecmw_read_bin( fp, "S", label )) {
947  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
948  return -1;
949  }
950  result->elem_label[i] = HECMW_strdup(label);
951  if(result->elem_label[i] == NULL) {
952  HECMW_set_error(errno, "(label)");
953  return -1;
954  }
955  }
956 
957  /* elem_val_item */
959  if(ResIO.elem_global_ID == NULL) {
960  HECMW_set_error(errno, "(elem_global_ID)");
961  return -1;
962  }
963  result->elem_val_item = HECMW_malloc(sizeof(*result->elem_val_item)*n*n_elem);
964  if(result->elem_val_item == NULL) {
965  HECMW_set_error(errno, "(elem_val_item)");
966  return -1;
967  }
968 
969  m = 0;
970  for(i=0; i < n_elem; i++) {
971  if(hecmw_read_bin( fp, "I", &ResIO.elem_global_ID[i] )) {
972  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
973  return -1;
974  }
975  for(j=0; j < result->ne_component; j++) {
976  for(k=0; k < result->ne_dof[j]; k++) {
977  if(hecmw_read_bin( fp, "F", &result->elem_val_item[m] )) {
978  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
979  return -1;
980  }
981  m++;
982  }
983  }
984 
985  }
986 
987  return 0;
988 }
989 
990 
991 static struct hecmwST_result_data *bin_input_result_data(FILE *fp) {
992  int n_node, n_elem;
993  struct hecmwST_result_data *result;
994 
995  HECMW_assert(fp);
996 
997  result = HECMW_calloc(1, sizeof(*result));
998  if(result == NULL) {
999  HECMW_set_error(errno, "");
1000  return NULL;
1001  }
1002 
1003  if(bin_input_result_header(result, fp)) {
1004  return NULL;
1005  }
1006 
1007  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
1008  if(bin_input_result_global(result, fp)) {
1009  return NULL;
1010  }
1011  }
1012  if(bin_input_result_dataheader(result, &n_node, &n_elem, fp)) {
1013  return NULL;
1014  }
1015  ResIO.nnode = n_node;
1016  ResIO.nelem = n_elem;
1017 
1018  if(bin_input_result_node(result, n_node, fp)) {
1019  return NULL;
1020  }
1021 
1022  if(bin_input_result_elem(result, n_elem, fp)) {
1023  return NULL;
1024  }
1025 
1026  return result;
1027 }
1028 
1029 
1030 /*---------------------------------------------------------------------------*/
1031 
1032 
1034  FILE *fp;
1035  struct hecmwST_result_data *result;
1036 
1037  if((fp = fopen(filename, "rb")) == NULL) {
1038  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename, HECMW_strmsg(errno));
1039  return NULL;
1040  }
1041 
1043 
1044  if(!check_bin_header(fp)) {
1045  fclose(fp);
1046  HECMW_set_error(HECMW_UTIL_E0202, "%s is not binary result file", filename);
1047  return NULL;
1048  }
1049  result = bin_input_result_data(fp);
1050  if(result == NULL) {
1051  return NULL;
1052  }
1053  if(fclose(fp)) {
1055  return NULL;
1056  }
1057 
1058  return result;
1059 }
void hecmw_set_endian_info(void)
Definition: hecmw_bin_io.c:53
int hecmw_write_bin(FILE *fp, const char *fmt,...)
Definition: hecmw_bin_io.c:240
int hecmw_read_bin(FILE *fp, const char *fmt,...)
Definition: hecmw_bin_io.c:322
#define HECMW_MSG_LEN
Definition: hecmw_config.h:76
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:70
#define HECMW_NAME_LEN
Definition: hecmw_config.h:72
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:33
#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_E0205
Definition: hecmw_msgno.h:372
#define HECMW_UTIL_E0201
Definition: hecmw_msgno.h:368
#define HECMW_UTIL_E0202
Definition: hecmw_msgno.h:369
int HECMW_result_io_count_ng_comp(void)
int HECMW_result_io_count_ne_comp(void)
int HECMW_result_io_count_nn_comp(void)
struct hecmwST_result_io_data ResIO
#define HECMW_RESULT_FILEVER_MINOR
#define HECMW_RESULT_FILEVER_MAJOR
#define LINEBUF_SIZE
int HECMW_result_io_bin_judge_file(char *filename)
#define RES_BIN_HEADER
struct hecmwST_result_data * HECMW_result_io_bin_read_by_fname(char *filename)
int HECMW_result_io_bin_write_ST_by_fname(char *filename, struct hecmwST_result_data *result, int n_node, int n_elem, char *header, char *comment)
int HECMW_result_io_bin_write_by_fname(char *filename)
#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
char comment_line[HECMW_MSG_LEN+1]
struct result_list * global_list
struct result_list * node_list
char head[HECMW_HEADER_LEN+1]
struct result_list * elem_list
double * ptr
struct result_list * next