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