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