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