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  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  char tmp[HECMW_HEADER_LEN+1];
407  snprintf(tmp, sizeof(tmp), "%s %d.%d", head, HECMW_RESULT_FILEVER_MAJOR, HECMW_RESULT_FILEVER_MINOR);
408  memcpy(head, tmp, sizeof(head));
409  }
410  rc = hecmw_write_bin(fp, "S", header);
411  if(rc < 0) {
413  return -1;
414  }
415 
416  return 0;
417 }
418 
419 
420 static int bin_output_result_global_ST(struct hecmwST_result_data *result,
421  char *comment, FILE *fp) {
422  size_t len;
423  int i,j,k,n,m,rc;
424  char *p,*q;
425  char comment_line[HECMW_MSG_LEN+1];
426 
427  if(comment == NULL) {
428  comment_line[0] = '\0';
429  } else {
430  len = 0;
431  p = comment;
432  q = comment_line;
433  while(len < sizeof(comment_line)-1 && *p && *p != '\n') {
434  *q++ = *p++;
435  len++;
436  }
437  *q++ = '\0';
438  }
439 
440  /* comment */
441  rc = hecmw_write_bin(fp,"S","*comment");
442  if(rc < 0) {
443  HECMW_set_error(HECMW_UTIL_E0205, "*comment");
444  return -1;
445  }
446  rc = hecmw_write_bin(fp, "S", comment);
447  if(rc < 0) {
448  HECMW_set_error(HECMW_UTIL_E0205, "comment");
449  return -1;
450  }
451 
452  /* global header */
453  rc = hecmw_write_bin(fp,"S","*global");
454  if(rc < 0) {
455  HECMW_set_error(HECMW_UTIL_E0205, "*global");
456  return -1;
457  }
458 
459  /* ng_component */
460  rc = hecmw_write_bin(fp, "II", result->ng_component);
461  if(rc < 0) {
462  HECMW_set_error(HECMW_UTIL_E0205, "ng_comp");
463  return -1;
464  }
465 
466  /* ng_dof */
467  n = 0;
468  for(i=0; i < result->ng_component; i++) {
469  rc = hecmw_write_bin(fp, "I", result->ng_dof[i] );
470  if(rc < 0) {
472  return -1;
473  }
474  n++;
475  }
476 
477  /* global_label */
478  for(i=0; i < result->ng_component; i++) {
479  rc = hecmw_write_bin(fp, "S", result->global_label[i]);
480  if(rc < 0) {
481  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
482  return -1;
483  }
484  }
485 
486  /* global_val_item */
487  if(result->ng_component == 0) return 0;
488  m = 0;
489  for(j=0; j < result->ng_component; j++) {
490  for(k=0; k < result->ng_dof[j]; k++) {
491  rc = hecmw_write_bin(fp, "F", result->global_val_item[m] );
492  if(rc < 0) {
493  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
494  return -1;
495  }
496  m++;
497  }
498  }
499 
500  /* data header */
501  rc = hecmw_write_bin(fp,"S","*data");
502  if(rc < 0) {
504  return -1;
505  }
506 
507  return 0;
508 }
509 
510 
511 static int bin_output_result_dataheader_ST(struct hecmwST_result_data *result,
512  int n_node, int n_elem, FILE *fp) {
513  int rc;
514 
515  /* n_node, n_elem */
516  rc = hecmw_write_bin(fp, "II", n_node, n_elem);
517  if(rc < 0) {
518  HECMW_set_error(HECMW_UTIL_E0205, "n_node,n_elem");
519  return -1;
520  }
521 
522  /* nn_component, ne_component */
523  rc = hecmw_write_bin(fp, "II", result->nn_component, result->ne_component);
524  if(rc < 0) {
525  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
526  return -1;
527  }
528 
529  return 0;
530 }
531 
532 
533 static int bin_output_result_node_ST(struct hecmwST_result_data *result,
534  int n_node, FILE *fp) {
535  int i,j,k,n,m,rc;
536 
537  /* nn_dof */
538  n = 0;
539  for(i=0; i < result->nn_component; i++) {
540  rc = hecmw_write_bin(fp, "I", result->nn_dof[i] );
541  if(rc < 0) {
543  return -1;
544  }
545  n++;
546  }
547 
548  /* node_label */
549  for(i=0; i < result->nn_component; i++) {
550  rc = hecmw_write_bin(fp, "S", result->node_label[i]);
551  if(rc < 0) {
552  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
553  return -1;
554  }
555  }
556 
557  /* node_val_item */
558  if(result->nn_component == 0) return 0;
559  m = 0;
560  for(i=0; i < n_node; i++) {
561  rc = hecmw_write_bin(fp, "I", ResIO.node_global_ID[i] );
562  if(rc < 0) {
563  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
564  return -1;
565  }
566  for(j=0; j < result->nn_component; j++) {
567  for(k=0; k < result->nn_dof[j]; k++) {
568  rc = hecmw_write_bin(fp, "F", result->node_val_item[m] );
569  if(rc < 0) {
570  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
571  return -1;
572  }
573  m++;
574  }
575  }
576  }
577 
578  return 0;
579 }
580 
581 
582 static int bin_output_result_elem_ST(struct hecmwST_result_data *result,
583  int n_elem, FILE *fp) {
584  int i,j,k,n,m,rc;
585 
586  /* ne_dof */
587  n = 0;
588  for(i=0; i < result->ne_component; i++) {
589  rc = hecmw_write_bin(fp, "I", result->ne_dof[i] );
590  if(rc < 0) {
592  return -1;
593  }
594  n++;
595  }
596 
597  /* elem_label */
598  for(i=0; i < result->ne_component; i++) {
599  rc = hecmw_write_bin(fp, "S", result->elem_label[i]);
600  if(rc < 0) {
601  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
602  return -1;
603  }
604  }
605 
606  /* elem_val_item */
607  if(result->ne_component == 0) return 0;
608  m = 0;
609  for(i=0; i < n_elem; i++) {
610  rc = hecmw_write_bin(fp, "I", ResIO.elem_global_ID[i] );
611  if(rc < 0) {
612  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
613  return -1;
614  }
615  for(j=0; j < result->ne_component; j++) {
616  for(k=0; k < result->ne_dof[j]; k++) {
617  rc = hecmw_write_bin(fp, "F", result->elem_val_item[m]);
618  if(rc < 0) {
619  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
620  return -1;
621  }
622  m++;
623  }
624  }
625  }
626 
627  return 0;
628 }
629 
630 
631 static int bin_output_result_data_ST(struct hecmwST_result_data *result,
632  int n_node, int n_elem, char *header,
633  char *comment, FILE *fp) {
634  HECMW_assert(fp);
635 
636  if(bin_output_result_header_ST(result, header, fp)) {
637  return -1;
638  }
639  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
640  if(bin_output_result_global_ST(result, comment, fp)) {
641  return -1;
642  }
643  }
644  if(bin_output_result_dataheader_ST(result, n_node, n_elem, fp)) {
645  return -1;
646  }
647  if(bin_output_result_node_ST(result, n_node, fp)) {
648  return -1;
649  }
650  if(bin_output_result_elem_ST(result, n_elem, fp)) {
651  return -1;
652  }
653 
654  return 0;
655 }
656 
657 /*---------------------------------------------------------------------------*/
658 
660  struct hecmwST_result_data *result,
661  int n_node, int n_elem, char *header, char *comment) {
662  FILE *fp = NULL;
663 
664  if (HECMW_ctrl_is_subdir()) {
665  if (HECMW_ctrl_make_subdir(filename)) {
666  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
667  HECMW_strmsg(errno));
668  goto error;
669  }
670  }
671 
672  if ((fp = fopen(filename, "wb")) == NULL) {
673  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename,
674  HECMW_strmsg(errno));
675  goto error;
676  }
677 
679  if (write_bin_header(fp)) goto error;
680  if (bin_output_result_data_ST(result, n_node, n_elem, header, comment, fp)) goto error;
681 
682  if (fclose(fp)) {
684  goto error;
685  }
686  fp = NULL;
687 
688  return 0;
689 error:
690  if (fp) fclose(fp);
691  return -1;
692 }
693 
694 
695 /*---------------------------------------------------------------------------*/
696 /* BINARY MODE I/O --- bin_input_result */
697 /*---------------------------------------------------------------------------*/
698 
699 
700 static int bin_input_result_header(struct hecmwST_result_data *result, FILE *fp) {
701  char *ptr;
702 
703  /* header */
704  if(hecmw_read_bin(fp, "S", Line_Buf)) {
706  return -1;
707  }
708  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
709  ptr = strtok(Line_Buf, " ");
710  if(ptr != Line_Buf) {
711  size_t len = strlen(ptr);
712  memmove(Line_Buf, ptr, len + 1);
713  } else {
714  ptr[strlen(ptr)] = '\0';
715  }
716  }
717  strcpy( ResIO.head, Line_Buf );
718 
719  return 0;
720 }
721 
722 
723 static int bin_input_result_global(struct hecmwST_result_data *result, FILE *fp) {
724  int i,j,k,n,m;
725  /* comment */
726  if(hecmw_read_bin(fp, "S", Line_Buf)) { //skip comment header
727  HECMW_set_error(HECMW_UTIL_E0205, "comment");
728  return -1;
729  }
730  if(hecmw_read_bin(fp, "S", Line_Buf)) {
731  HECMW_set_error(HECMW_UTIL_E0205, "comment");
732  return -1;
733  }
734  strcpy( ResIO.comment_line, Line_Buf );
735 
736  /* skip global header */
737  if(hecmw_read_bin(fp, "S", Line_Buf)) {
738  HECMW_set_error(HECMW_UTIL_E0205, "*global");
739  return -1;
740  }
741 
742  /* ng_component */
743  if(hecmw_read_bin(fp, "II", &result->ng_component)) {
744  HECMW_set_error(HECMW_UTIL_E0205, "ng_component");
745  return -1;
746  }
747 
748  if(result->ng_component <= 0) {
749  return 0;
750  }
751 
752  /* ng_dof */
753  result->ng_dof = HECMW_malloc(sizeof(*result->ng_dof)*result->ng_component);
754  if(result->ng_dof == NULL) {
755  HECMW_set_error(errno, "");
756  return -1;
757  }
758 
759  n = 0;
760  for(i=0; i<result->ng_component; i++){
761  if(hecmw_read_bin( fp, "I", &result->ng_dof[i] )) {
763  return -1;
764  }
765  n += result->ng_dof[i];
766  }
767 
768  /* global_label */
769  result->global_label = HECMW_malloc(sizeof(*result->global_label)*result->ng_component);
770  if(result->global_label == NULL) {
771  HECMW_set_error(errno, "(global_label)");
772  return -1;
773  }
774 
775  for(i=0; i < result->ng_component; i++) {
776  char label[HECMW_NAME_LEN+1];
777  if(hecmw_read_bin( fp, "S", label )) {
778  HECMW_set_error(HECMW_UTIL_E0205, "global_label");
779  return -1;
780  }
781  result->global_label[i] = HECMW_strdup(label);
782  if(result->global_label[i] == NULL) {
783  HECMW_set_error(errno, "(label)");
784  return -1;
785  }
786  }
787 
788  /* global_val_item */
789  result->global_val_item = HECMW_malloc(sizeof(*result->global_val_item)*n);
790  if(result->global_val_item == NULL) {
791  HECMW_set_error(errno, "(global_val_item)");
792  return -1;
793  }
794 
795  m = 0;
796  for(j=0; j < result->ng_component; j++) {
797  for(k=0; k < result->ng_dof[j]; k++) {
798  if(hecmw_read_bin( fp, "F", &result->global_val_item[m] )) {
799  HECMW_set_error(HECMW_UTIL_E0205, "global_val_item");
800  return -1;
801  }
802  m++;
803  }
804  }
805 
806  /* skip data header */
807  if(hecmw_read_bin(fp, "S", Line_Buf)) {
809  return -1;
810  }
811 
812  return 0;
813 }
814 
815 
816 static int bin_input_result_dataheader(struct hecmwST_result_data *result,
817  int *n_node, int *n_elem, FILE *fp) {
818  int nn, ne;
819 
820  /* n_node, n_elem */
821  if(hecmw_read_bin(fp, "II", &nn, &ne)) {
822  HECMW_set_error(HECMW_UTIL_E0205, "n_node,n_elem");
823  return -1;
824  }
825  *n_node = nn;
826  *n_elem = ne;
827 
828  /* nn_component, ne_component */
829  if(hecmw_read_bin(fp, "II", &result->nn_component, &result->ne_component)) {
830  HECMW_set_error(HECMW_UTIL_E0205, "nn_comp,ne_comp");
831  return -1;
832  }
833 
834  return 0;
835 }
836 
837 
838 static int bin_input_result_node(struct hecmwST_result_data *result, int n_node, FILE *fp) {
839  int i,j,k,n,m;
840 
841  if(result->nn_component <= 0) {
842  return 0;
843  }
844 
845  /* nn_dof */
846  result->nn_dof = HECMW_malloc(sizeof(*result->nn_dof)*result->nn_component);
847  if(result->nn_dof == NULL) {
848  HECMW_set_error(errno, "");
849  return -1;
850  }
851 
852  n = 0;
853  for(i=0; i<result->nn_component; i++){
854  if(hecmw_read_bin( fp, "I", &result->nn_dof[i] )) {
856  return -1;
857  }
858  n += result->nn_dof[i];
859  }
860 
861  /* node_label */
862  result->node_label = HECMW_malloc(sizeof(*result->node_label)*result->nn_component);
863  if(result->node_label == NULL) {
864  HECMW_set_error(errno, "(node_label)");
865  return -1;
866  }
867 
868  for(i=0; i < result->nn_component; i++) {
869  char label[HECMW_NAME_LEN+1];
870  if(hecmw_read_bin( fp, "S", label )) {
871  HECMW_set_error(HECMW_UTIL_E0205, "node_label");
872  return -1;
873  }
874  result->node_label[i] = HECMW_strdup(label);
875  if(result->node_label[i] == NULL) {
876  HECMW_set_error(errno, "(label)");
877  return -1;
878  }
879  }
880 
881  /* node_val_item */
883  if(ResIO.node_global_ID == NULL) {
884  HECMW_set_error(errno, "(node_global_ID)");
885  return -1;
886  }
887  result->node_val_item = HECMW_malloc(sizeof(*result->node_val_item)*n*n_node);
888  if(result->node_val_item == NULL) {
889  HECMW_set_error(errno, "(node_val_item)");
890  return -1;
891  }
892 
893  m = 0;
894  for(i=0; i < n_node; i++) {
895  if(hecmw_read_bin( fp, "I", &ResIO.node_global_ID[i] )) {
896  HECMW_set_error(HECMW_UTIL_E0205, "node_global_ID");
897  return -1;
898  }
899  for(j=0; j < result->nn_component; j++) {
900  for(k=0; k < result->nn_dof[j]; k++) {
901  if(hecmw_read_bin( fp, "F", &result->node_val_item[m] )) {
902  HECMW_set_error(HECMW_UTIL_E0205, "node_val_item");
903  return -1;
904  }
905  m++;
906  }
907  }
908 
909  }
910 
911  return 0;
912 }
913 
914 
915 static int bin_input_result_elem(struct hecmwST_result_data *result, int n_elem, FILE *fp) {
916  int i,j,k,n,m;
917 
918  if(result->ne_component <= 0) {
919  return 0;
920  }
921 
922  /* ne_dof */
923  result->ne_dof = HECMW_malloc(sizeof(*result->ne_dof)*result->ne_component);
924  if(result->ne_dof == NULL) {
925  HECMW_set_error(errno, "(ne_dof)");
926  return -1;
927  }
928 
929  n = 0;
930  for(i=0; i<result->ne_component;i++ ){
931  if(hecmw_read_bin( fp, "I", &result->ne_dof[i] )) {
933  return -1;
934  }
935  n += result->ne_dof[i];
936  }
937 
938  /* elem_label */
939  result->elem_label = HECMW_malloc(sizeof(*result->elem_label)*result->ne_component);
940  if(result->elem_label == NULL) {
941  HECMW_set_error(errno, "(elem_label)");
942  return -1;
943  }
944 
945  for(i=0; i < result->ne_component; i++) {
946  char label[HECMW_NAME_LEN+1];
947  if(hecmw_read_bin( fp, "S", label )) {
948  HECMW_set_error(HECMW_UTIL_E0205, "elem_label");
949  return -1;
950  }
951  result->elem_label[i] = HECMW_strdup(label);
952  if(result->elem_label[i] == NULL) {
953  HECMW_set_error(errno, "(label)");
954  return -1;
955  }
956  }
957 
958  /* elem_val_item */
960  if(ResIO.elem_global_ID == NULL) {
961  HECMW_set_error(errno, "(elem_global_ID)");
962  return -1;
963  }
964  result->elem_val_item = HECMW_malloc(sizeof(*result->elem_val_item)*n*n_elem);
965  if(result->elem_val_item == NULL) {
966  HECMW_set_error(errno, "(elem_val_item)");
967  return -1;
968  }
969 
970  m = 0;
971  for(i=0; i < n_elem; i++) {
972  if(hecmw_read_bin( fp, "I", &ResIO.elem_global_ID[i] )) {
973  HECMW_set_error(HECMW_UTIL_E0205, "elem_global_ID");
974  return -1;
975  }
976  for(j=0; j < result->ne_component; j++) {
977  for(k=0; k < result->ne_dof[j]; k++) {
978  if(hecmw_read_bin( fp, "F", &result->elem_val_item[m] )) {
979  HECMW_set_error(HECMW_UTIL_E0205, "elem_val_item");
980  return -1;
981  }
982  m++;
983  }
984  }
985 
986  }
987 
988  return 0;
989 }
990 
991 
992 static struct hecmwST_result_data *bin_input_result_data(FILE *fp) {
993  int n_node, n_elem;
994  struct hecmwST_result_data *result;
995 
996  HECMW_assert(fp);
997 
998  result = HECMW_calloc(1, sizeof(*result));
999  if(result == NULL) {
1000  HECMW_set_error(errno, "");
1001  return NULL;
1002  }
1003 
1004  if(bin_input_result_header(result, fp)) {
1005  return NULL;
1006  }
1007 
1008  if( HECMW_RESULT_FILEVER_MAJOR > 1 ){
1009  if(bin_input_result_global(result, fp)) {
1010  return NULL;
1011  }
1012  }
1013  if(bin_input_result_dataheader(result, &n_node, &n_elem, fp)) {
1014  return NULL;
1015  }
1016  ResIO.nnode = n_node;
1017  ResIO.nelem = n_elem;
1018 
1019  if(bin_input_result_node(result, n_node, fp)) {
1020  return NULL;
1021  }
1022 
1023  if(bin_input_result_elem(result, n_elem, fp)) {
1024  return NULL;
1025  }
1026 
1027  return result;
1028 }
1029 
1030 
1031 /*---------------------------------------------------------------------------*/
1032 
1033 
1035  FILE *fp;
1036  struct hecmwST_result_data *result;
1037 
1038  if((fp = fopen(filename, "rb")) == NULL) {
1039  HECMW_set_error(HECMW_UTIL_E0201, "File: %s, %s", filename, HECMW_strmsg(errno));
1040  return NULL;
1041  }
1042 
1044 
1045  if(!check_bin_header(fp)) {
1046  fclose(fp);
1047  HECMW_set_error(HECMW_UTIL_E0202, "%s is not binary result file", filename);
1048  return NULL;
1049  }
1050  result = bin_input_result_data(fp);
1051  if(result == NULL) {
1052  return NULL;
1053  }
1054  if(fclose(fp)) {
1056  return NULL;
1057  }
1058 
1059  return result;
1060 }
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: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