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