FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_io_dist.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 <errno.h>
9 #include <ctype.h>
10 #include <limits.h>
11 #include "hecmw_struct.h"
12 #include "hecmw_util.h"
13 #include "hecmw_dist_alloc.h"
14 #include "hecmw_io_dist.h"
15 
16 #define FSCANF_RTC_TOO_SHORT (-10001)
17 #define FSCNAF_RTC_EOF (-10002)
18 
19 #define COLS_INT_DEF 10
20 #define COLS_DOUBLE_DEF 5
21 #define COLS_TWO 2
22 #define COLS_THREE 3
23 
24 #define FILE_MAGIC "!HECMW-DMD-ASCII"
25 #define FILE_MAGIC_LEN (sizeof(FILE_MAGIC) - 1)
26 #define HEADER_STRING "!HECMW-DMD-ASCII version="
27 
28 /* */
29 /* get data */
30 /* */
31 
32 /*----------------------------------------------------------------------------*/
33 /* integer */
34 /*----------------------------------------------------------------------------*/
35 static int get_int(int *i, FILE *fp) {
36  int rtc;
37 
38  rtc = fscanf(fp, "%d", i);
39  if (rtc < 1) {
41  return -1;
42  } else if (rtc == EOF) {
44  return -1;
45  } else {
46  return 0;
47  }
48 }
49 
50 /*----------------------------------------------------------------------------*/
51 /* double */
52 /*----------------------------------------------------------------------------*/
53 static int get_double(double *d, FILE *fp) {
54  int rtc;
55 
56  rtc = fscanf(fp, "%lf", d);
57  if (rtc < 1) {
59  return -1;
60  } else if (rtc == EOF) {
62  return -1;
63  } else {
64  return 0;
65  }
66 }
67 
68 /*----------------------------------------------------------------------------*/
69 /* string */
70 /*----------------------------------------------------------------------------*/
71 static int get_string(char *s, int max, FILE *fp) {
72  int c, len;
73 
74  while ((c = fgetc(fp)) != EOF && isspace(c))
75  ; /* skip */
76  if (c == EOF) {
78  return -1;
79  }
80  if (ungetc(c, fp) == EOF) {
82  return -1;
83  }
84  if (fgets(s, max, fp) == NULL) {
86  return -1;
87  }
88  len = strlen(s);
89  if (len == max - 1 && s[max - 2] != '\n') {
90  HECMW_set_error(HECMW_IO_E5004, "line too long");
91  return -1;
92  }
93  while (len > 0 && isspace(s[len - 1])) {
94  len--;
95  }
96  s[len] = '\0';
97 
98  return strlen(s);
99 }
100 
101 /*----------------------------------------------------------------------------*/
102 /* HECMW_Comm */
103 /*----------------------------------------------------------------------------*/
104 static int get_comm(HECMW_Comm *i, FILE *fp) {
105  int rtc;
106 
107  rtc = fscanf(fp, "%d", (int *)i);
108  if (rtc < 1) {
110  return -1;
111  } else if (rtc == EOF) {
113  return -1;
114  } else {
115  return 0;
116  }
117 }
118 
119 /*----------------------------------------------------------------------------*/
120 /* array ( int ) */
121 /*----------------------------------------------------------------------------*/
122 static int get_int_ary(int *ary, int n, FILE *fp) {
123  int rtc, i;
124 
125  for (i = 0; i < n; i++) {
126  rtc = fscanf(fp, "%d", &ary[i]);
127  if (rtc < 1) {
129  return -1;
130  } else if (rtc == EOF) {
132  return -1;
133  }
134  }
135 
136  return 0;
137 }
138 
139 /*----------------------------------------------------------------------------*/
140 /* Generic: read int array and convert to long long array */
141 /*----------------------------------------------------------------------------*/
142 static int get_int_to_longlong_ary(long long *ary, size_t n, FILE *fp) {
143  int rtc;
144  size_t i;
145  int tmp;
146 
147  for (i = 0; i < n; i++) {
148  rtc = fscanf(fp, "%d", &tmp);
149  if (rtc < 1) {
151  return -1;
152  } else if (rtc == EOF) {
154  return -1;
155  }
156  ary[i] = (long long)tmp;
157  }
158 
159  return 0;
160 }
161 
162 /*----------------------------------------------------------------------------*/
163 /* array ( double ) */
164 /*----------------------------------------------------------------------------*/
165 static int get_double_ary(double *ary, int n, FILE *fp) {
166  int rtc, i;
167 
168  for (i = 0; i < n; i++) {
169  rtc = fscanf(fp, "%lf", &ary[i]);
170  if (rtc < 1) {
172  return -1;
173  } else if (rtc == EOF) {
175  return -1;
176  }
177  }
178 
179  return 0;
180 }
181 
182 /*----------------------------------------------------------------------------*/
183 /* array ( string ) */
184 /*----------------------------------------------------------------------------*/
185 static int get_string_ary(char **ary, int n, FILE *fp) {
186 #define LINEBUF_SIZE 8096
187  int i;
188  char linebuf[LINEBUF_SIZE];
189 
190  for (i = 0; i < n; i++) {
191  if (get_string(linebuf, sizeof(linebuf), fp) < 0) {
192  return -1;
193  }
194  if ((ary[i] = (char *)HECMW_strdup(linebuf)) == NULL) {
195  HECMW_set_error(errno, "");
196  return -1;
197  }
198  }
199 
200  return 0;
201 }
202 
203 /*============================================================================*/
204 /* */
205 /* check file type */
206 /* */
207 /*============================================================================*/
208 static int is_hecmw_dist_file(FILE *fp) {
209  char filetype[FILE_MAGIC_LEN];
210 
211  if (fread(filetype, FILE_MAGIC_LEN, 1, fp) != 1) {
213  return 0;
214  }
215  if (memcmp(filetype, FILE_MAGIC, FILE_MAGIC_LEN)) {
216  HECMW_set_error(HECMW_IO_E5005, "Not a HECMW-DIST ASCII file");
217  return 0;
218  }
219  return 1;
220 }
221 
222 /*============================================================================*/
223 /* */
224 /* rewind FILE pointer */
225 /* */
226 /*============================================================================*/
227 static int rewind_fp(FILE *fp) {
228  if (fseek(fp, 0L, SEEK_SET)) {
230  return -1;
231  }
232  return 0;
233 }
234 
235 /*============================================================================*/
236 /* */
237 /* get header */
238 /* */
239 /*============================================================================*/
240 static int get_header(FILE *fp) {
241  char header[HECMW_HEADER_LEN + 1];
242  int ver;
243 
244  if (fgets(header, sizeof(header), fp) == NULL) {
246  return -1;
247  }
248  if (strlen(header) == sizeof(header) - 1 &&
249  header[strlen(header) - 2] != '\n') {
250  HECMW_set_error(HECMW_IO_E5004, "line too long");
251  return -1;
252  }
253  if (strncmp(header, HEADER_STRING, strlen(HEADER_STRING)) != 0) {
254  HECMW_set_error(HECMW_IO_E5005, "Not a HECMW-DIST file");
255  return -1;
256  }
257  if (sscanf(header + strlen(HEADER_STRING), "%d", &ver) != 1) {
258  HECMW_set_error(HECMW_IO_E5006, "Invalid version");
259  return -1;
260  }
261  return 0;
262 }
263 
264 /*============================================================================*/
265 /* */
266 /* get global information */
267 /* */
268 /*============================================================================*/
269 static int get_global_info(struct hecmwST_local_mesh *mesh, FILE *fp) {
270  int flag_header;
271 
272  /* hecmw_flag_adapt */
273  if (get_int(&mesh->hecmw_flag_adapt, fp)) {
274  return -1;
275  }
276 
277  /* hecmw_flag_initcon */
278  if (get_int(&mesh->hecmw_flag_initcon, fp)) {
279  return -1;
280  }
281 
282  /* hecmw_flag_parttype */
283  if (get_int(&mesh->hecmw_flag_parttype, fp)) {
284  return -1;
285  }
286 
287  /* hecmw_flag_partdepth */
288  if (get_int(&mesh->hecmw_flag_partdepth, fp)) {
289  return -1;
290  }
291 
292  /* hecmw_flag_version */
293  if (get_int(&mesh->hecmw_flag_version, fp)) {
294  return -1;
295  }
296 
297  /* hecmw_flag_partcontact */
298  if (mesh->hecmw_flag_version >= 4) {
299  if (get_int(&mesh->hecmw_flag_partcontact, fp)) {
300  return -1;
301  }
302  } else {
304  }
305 
306  /* gridfile */
307  if (get_string(mesh->gridfile, sizeof(mesh->gridfile), fp) < 0) {
308  return -1;
309  }
310 
311  /* hecmw_n_file */
312  if (get_int(&mesh->hecmw_n_file, fp)) {
313  return -1;
314  }
315 
316  /* files */
317  if (mesh->hecmw_n_file > 0) {
318  if ((mesh->files = (char **)HECMW_calloc(mesh->hecmw_n_file,
319  sizeof(char *))) == NULL) {
320  HECMW_set_error(errno, "");
321  return -1;
322  }
323  if (get_string_ary(mesh->files, mesh->hecmw_n_file, fp)) {
324  return -1;
325  }
326  } else {
327  mesh->files = NULL;
328  }
329 
330  /* flag for header */
331  if (get_int(&flag_header, fp)) {
332  return -1;
333  }
334 
335  if (flag_header == 1) {
336  /* header */
337  if (get_string(mesh->header, sizeof(mesh->header), fp) < 0) {
338  return -1;
339  }
340  }
341 
342  /* zero_temp */
343  if (get_double(&mesh->zero_temp, fp)) {
344  return -1;
345  }
346 
347  return 0;
348 }
349 
350 /*============================================================================*/
351 /* */
352 /* get node information */
353 /* */
354 /*============================================================================*/
355 static int get_node_info(struct hecmwST_local_mesh *mesh, FILE *fp) {
356  /* n_node*/
357  if (get_int(&mesh->n_node, fp)) {
358  return -1;
359  }
360 
361  if (mesh->hecmw_flag_version >= 2) {
362  /* n_node_gross*/
363  if (get_int(&mesh->n_node_gross, fp)) {
364  return -1;
365  }
366  } else {
368  }
369 
370  if (mesh->hecmw_flag_version >= 4) {
371  /* nn_middle*/
372  if (get_int(&mesh->nn_middle, fp)) {
373  return -1;
374  }
375  } else {
377  }
378 
379  /* nn_internal */
380  if (get_int(&mesh->nn_internal, fp)) {
381  return -1;
382  }
383 
384  /* node_internal_list */
387  mesh->nn_internal > 0) {
389  (int *)HECMW_malloc(sizeof(double) * mesh->nn_internal);
390  if (mesh->node_internal_list == NULL) {
391  HECMW_set_error(errno, "");
392  return -1;
393  }
394  if (get_int_ary(mesh->node_internal_list, mesh->nn_internal, fp)) {
395  return -1;
396  }
397  }
398 
399  if (mesh->n_node_gross > 0) {
400  /* node_ID */
401  mesh->node_ID = (int *)HECMW_malloc(sizeof(int) * mesh->n_node_gross * 2);
402  if (mesh->node_ID == NULL) {
403  HECMW_set_error(errno, "");
404  return -1;
405  }
406  if (get_int_ary(mesh->node_ID, mesh->n_node_gross * 2, fp)) {
407  return -1;
408  }
409 
410  /* global_node_ID */
412  (int *)HECMW_malloc(sizeof(int) * mesh->n_node_gross);
413  if (mesh->global_node_ID == NULL) {
414  HECMW_set_error(errno, "");
415  return -1;
416  }
417  if (get_int_ary(mesh->global_node_ID, mesh->n_node_gross, fp)) {
418  return -1;
419  }
420 
421  /* node */
422  mesh->node =
423  (double *)HECMW_malloc(sizeof(double) * mesh->n_node_gross * 3);
424  if (mesh->node == NULL) {
425  HECMW_set_error(errno, "");
426  return -1;
427  }
428  if (get_double_ary(mesh->node, mesh->n_node_gross * 3, fp)) {
429  return -1;
430  }
431  }
432 
433  /* n_dof */
434  if (get_int(&mesh->n_dof, fp)) {
435  return -1;
436  }
437 
438  /* n_dof_grp */
439  if (get_int(&mesh->n_dof_grp, fp)) {
440  return -1;
441  }
442 
443  if (mesh->n_dof_grp > 0) {
444  /* node_dof_index */
446  (int *)HECMW_malloc(sizeof(int) * (mesh->n_dof_grp + 1));
447  if (mesh->node_dof_index == NULL) {
448  HECMW_set_error(errno, "");
449  return -1;
450  }
451  if (get_int_ary(mesh->node_dof_index, mesh->n_dof_grp + 1, fp)) {
452  return -1;
453  }
454 
455  /* node_dof_item */
456  mesh->node_dof_item = (int *)HECMW_malloc(sizeof(int) * mesh->n_dof_grp);
457  if (mesh->node_dof_item == NULL) {
458  HECMW_set_error(errno, "");
459  return -1;
460  }
461  if (get_int_ary(mesh->node_dof_item, mesh->n_dof_grp, fp)) {
462  return -1;
463  }
464  }
465 
466  if (mesh->hecmw_flag_initcon && mesh->n_node_gross > 0) {
467  /* node_init_val_index */
469  (int *)HECMW_malloc(sizeof(int) * (mesh->n_node_gross + 1));
470  if (mesh->node_init_val_index == NULL) {
471  HECMW_set_error(errno, "");
472  return -1;
473  }
474  if (get_int_ary(mesh->node_init_val_index, mesh->n_node_gross + 1, fp)) {
475  return -1;
476  }
477 
478  /* node_init_val_item */
481  } else {
482  mesh->node_init_val_item = (double *)HECMW_malloc(
483  sizeof(double) * mesh->node_init_val_index[mesh->n_node_gross]);
484  if (mesh->node_init_val_item == NULL) {
485  HECMW_set_error(errno, "");
486  return -1;
487  }
488  if (get_double_ary(mesh->node_init_val_item,
490  return -1;
491  }
492  }
493  }
494 
495  return 0;
496 }
497 
498 /*============================================================================*/
499 /* */
500 /* get element information */
501 /* */
502 /*============================================================================*/
503 static int get_elem_info(struct hecmwST_local_mesh *mesh, FILE *fp) {
504  /* n_elem */
505  if (get_int(&mesh->n_elem, fp)) {
506  return -1;
507  }
508 
509  if (mesh->hecmw_flag_version >= 2) {
510  /* n_elem_gross */
511  if (get_int(&mesh->n_elem_gross, fp)) {
512  return -1;
513  }
514  } else {
516  }
517 
518  /* ne_internal */
519  if (get_int(&mesh->ne_internal, fp)) {
520  return -1;
521  }
522 
523  /* elem_internal_list */
526  mesh->ne_internal > 0) {
528  (int *)HECMW_malloc(sizeof(int) * mesh->ne_internal);
529  if (mesh->elem_internal_list == NULL) {
530  HECMW_set_error(errno, "");
531  return -1;
532  }
533  if (get_int_ary(mesh->elem_internal_list, mesh->ne_internal, fp)) {
534  return -1;
535  }
536  }
537 
538  if (mesh->n_elem_gross > 0) {
539  /* elem_ID */
540  if ((mesh->elem_ID = (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross *
541  2)) == NULL) {
542  HECMW_set_error(errno, "");
543  return -1;
544  }
545  if (get_int_ary(mesh->elem_ID, mesh->n_elem_gross * 2, fp)) {
546  return -1;
547  }
548 
549  /* global_elem_ID */
550  if ((mesh->global_elem_ID =
551  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
552  HECMW_set_error(errno, "");
553  return -1;
554  }
555  if (get_int_ary(mesh->global_elem_ID, mesh->n_elem_gross, fp)) {
556  return -1;
557  }
558 
559  /* elem_type */
560  if ((mesh->elem_type =
561  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
562  HECMW_set_error(errno, "");
563  return -1;
564  }
565  if (get_int_ary(mesh->elem_type, mesh->n_elem_gross, fp)) {
566  return -1;
567  }
568  }
569 
570  /* n_elem_type */
571  if (get_int(&mesh->n_elem_type, fp)) {
572  return -1;
573  }
574 
575  if (mesh->n_elem_type > 0) {
576  /* elem_type_index */
577  if ((mesh->elem_type_index = (int *)HECMW_malloc(
578  sizeof(int) * (mesh->n_elem_type + 1))) == NULL) {
579  HECMW_set_error(errno, "");
580  return -1;
581  }
582  if (get_int_ary(mesh->elem_type_index, mesh->n_elem_type + 1, fp)) {
583  return -1;
584  }
585 
586  /* elem_type_item */
587  if ((mesh->elem_type_item =
588  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_type)) == NULL) {
589  HECMW_set_error(errno, "");
590  return -1;
591  }
592  if (get_int_ary(mesh->elem_type_item, mesh->n_elem_type, fp)) {
593  return -1;
594  }
595  }
596 
597  if (mesh->n_elem_gross > 0) {
598  /* elem_node_index */
599  if ((mesh->elem_node_index = (long long *)HECMW_malloc(
600  sizeof(long long) * (mesh->n_elem_gross + 1))) == NULL) {
601  HECMW_set_error(errno, "");
602  return -1;
603  }
604  if (get_int_to_longlong_ary(mesh->elem_node_index, mesh->n_elem_gross + 1, fp)) {
605  return -1;
606  }
607 
608  /* elem_node_item */
609  if ((mesh->elem_node_item = (int *)HECMW_malloc(
610  sizeof(int) * mesh->elem_node_index[mesh->n_elem_gross])) ==
611  NULL) {
612  HECMW_set_error(errno, "");
613  return -1;
614  }
615  if (get_int_ary(mesh->elem_node_item,
617  return -1;
618  }
619 
620  /* section_ID */
621  if ((mesh->section_ID =
622  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
623  HECMW_set_error(errno, "");
624  return -1;
625  }
626  if (get_int_ary(mesh->section_ID, mesh->n_elem_gross, fp)) {
627  return -1;
628  }
629 
630  /* elem_mat_ID_index */
631  if ((mesh->elem_mat_ID_index = (int *)HECMW_malloc(
632  sizeof(int) * (mesh->n_elem_gross + 1))) == NULL) {
633  HECMW_set_error(errno, "");
634  return -1;
635  }
636  if (get_int_ary(mesh->elem_mat_ID_index, mesh->n_elem_gross + 1, fp)) {
637  return -1;
638  }
639 
640  /* elem_mat_ID_item */
641  if ((mesh->elem_mat_ID_item = (int *)HECMW_malloc(
642  sizeof(int) * mesh->elem_mat_ID_index[mesh->n_elem_gross])) ==
643  NULL) {
644  HECMW_set_error(errno, "");
645  return -1;
646  }
647  if (get_int_ary(mesh->elem_mat_ID_item,
649  return -1;
650  }
651  }
652 
653  /* n_elem_mat_ID */
654  if (get_int(&mesh->n_elem_mat_ID, fp)) {
655  return -1;
656  }
657 
658  /* elem_mat_int_index */
659  /* elem_mat_int_item */
660 
661  /* elem_val_index */
662  /* elem_val_item */
663 
664  return 0;
665 }
666 
667 /*============================================================================*/
668 /* */
669 /* get domain information & communication table */
670 /* */
671 /*============================================================================*/
672 static int get_comm_info(struct hecmwST_local_mesh *mesh, FILE *fp) {
673  /* zero */
674  if (get_int(&mesh->zero, fp)) {
675  return -1;
676  }
677 
678  /* HECMW_COMM */
679  if (get_comm(&mesh->HECMW_COMM, fp)) {
680  return -1;
681  }
682 
683  /* PETOT */
684  if (get_int(&mesh->PETOT, fp)) {
685  return -1;
686  }
687 
688  /* PEsmpTOT */
689  if (get_int(&mesh->PEsmpTOT, fp)) {
690  return -1;
691  }
692 
693  /* my_rank */
694  if (get_int(&mesh->my_rank, fp)) {
695  return -1;
696  }
697 
698  /* errnof */
699  if (get_int(&mesh->errnof, fp)) {
700  return -1;
701  }
702 
703  /* n_subdomain */
704  if (get_int(&mesh->n_subdomain, fp)) {
705  return -1;
706  }
707 
708  /* n_neighbor_pe */
709  if (get_int(&mesh->n_neighbor_pe, fp)) {
710  return -1;
711  }
712 
713  if (mesh->n_neighbor_pe == 0) {
714  mesh->neighbor_pe = NULL;
715  mesh->import_item = NULL;
716  mesh->export_item = NULL;
717  mesh->shared_item = NULL;
718 
719  mesh->import_index = HECMW_malloc(sizeof(int));
720  if (mesh->import_index == NULL) {
721  HECMW_set_error(errno, "");
722  return -1;
723  }
724  mesh->import_index[0] = 0;
725 
726  mesh->export_index = HECMW_malloc(sizeof(int));
727  if (mesh->export_index == NULL) {
728  HECMW_set_error(errno, "");
729  return -1;
730  }
731  mesh->export_index[0] = 0;
732 
733  mesh->shared_index = HECMW_malloc(sizeof(int));
734  if (mesh->shared_index == NULL) {
735  HECMW_set_error(errno, "");
736  return -1;
737  }
738  mesh->shared_index[0] = 0;
739 
740  return 0;
741  }
742 
743  /* neighbor_pe */
744  if ((mesh->neighbor_pe =
745  (int *)HECMW_malloc(sizeof(int) * mesh->n_neighbor_pe)) == NULL) {
746  HECMW_set_error(errno, "");
747  return -1;
748  }
749  if (get_int_ary(mesh->neighbor_pe, mesh->n_neighbor_pe, fp)) {
750  return -1;
751  }
752 
753  /* import_index */
754  if ((mesh->import_index = (int *)HECMW_malloc(
755  sizeof(int) * (mesh->n_neighbor_pe + 1))) == NULL) {
756  HECMW_set_error(errno, "");
757  return -1;
758  }
759  if (get_int_ary(mesh->import_index, mesh->n_neighbor_pe + 1, fp)) {
760  return -1;
761  }
762 
763  /* import_item */
764  if ((mesh->import_item = (int *)HECMW_malloc(
765  sizeof(int) * mesh->import_index[mesh->n_neighbor_pe])) == NULL) {
766  HECMW_set_error(errno, "");
767  return -1;
768  }
769  if (get_int_ary(mesh->import_item, mesh->import_index[mesh->n_neighbor_pe],
770  fp)) {
771  return -1;
772  }
773 
774  /* export_index */
775  if ((mesh->export_index = (int *)HECMW_malloc(
776  sizeof(int) * (mesh->n_neighbor_pe + 1))) == NULL) {
777  HECMW_set_error(errno, "");
778  return -1;
779  }
780  if (get_int_ary(mesh->export_index, mesh->n_neighbor_pe + 1, fp)) {
781  return -1;
782  }
783 
784  /* export_item */
785  if ((mesh->export_item = (int *)HECMW_malloc(
786  sizeof(int) * mesh->export_index[mesh->n_neighbor_pe])) == NULL) {
787  HECMW_set_error(errno, "");
788  return -1;
789  }
790  if (get_int_ary(mesh->export_item, mesh->export_index[mesh->n_neighbor_pe],
791  fp)) {
792  return -1;
793  }
794 
795  /* shared_index */
796  if ((mesh->shared_index = (int *)HECMW_malloc(
797  sizeof(int) * (mesh->n_neighbor_pe + 1))) == NULL) {
798  HECMW_set_error(errno, "");
799  return -1;
800  }
801  if (get_int_ary(mesh->shared_index, mesh->n_neighbor_pe + 1, fp)) {
802  return -1;
803  }
804 
805  /* shared_item */
806  if ((mesh->shared_item = (int *)HECMW_malloc(
807  sizeof(int) * mesh->shared_index[mesh->n_neighbor_pe])) == NULL) {
808  HECMW_set_error(errno, "");
809  return -1;
810  }
811  if (get_int_ary(mesh->shared_item, mesh->shared_index[mesh->n_neighbor_pe],
812  fp)) {
813  return -1;
814  }
815 
816  return 0;
817 }
818 
819 /*============================================================================*/
820 /* */
821 /* get adaptation information */
822 /* */
823 /*============================================================================*/
824 static int get_adapt_info(struct hecmwST_local_mesh *mesh, FILE *fp) {
825  if (mesh->hecmw_flag_adapt == 0) {
826  mesh->coarse_grid_level = 0;
827  mesh->n_adapt = 0;
831  mesh->adapt_type = NULL;
832  mesh->adapt_level = NULL;
836  return 0;
837  }
838 
839  /* coarse_grid_level */
840  if (get_int(&mesh->coarse_grid_level, fp)) {
841  return -1;
842  }
843 
844  /* n_adapt */
845  if (get_int(&mesh->n_adapt, fp)) {
846  return -1;
847  }
848 
849  if (mesh->n_node_gross > 0) {
850  /* when_i_was_refined_node */
852  (int *)HECMW_malloc(sizeof(int) * mesh->n_node_gross)) == NULL) {
853  HECMW_set_error(errno, "");
854  return -1;
855  }
856  if (get_int_ary(mesh->when_i_was_refined_node, mesh->n_node_gross, fp)) {
857  return -1;
858  }
859  }
860 
861  if (mesh->n_elem_gross <= 0) return 0;
862 
863  /* when_i_was_refined_elem */
865  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
866  HECMW_set_error(errno, "");
867  return -1;
868  }
869  if (get_int_ary(mesh->when_i_was_refined_elem, mesh->n_elem_gross, fp)) {
870  return -1;
871  }
872 
873  /* adapt_parent_type */
874  if ((mesh->adapt_parent_type =
875  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
876  HECMW_set_error(errno, "");
877  return -1;
878  }
879  if (get_int_ary(mesh->adapt_parent_type, mesh->n_elem_gross, fp)) {
880  return -1;
881  }
882 
883  /* adapt_type */
884  if ((mesh->adapt_type =
885  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
886  HECMW_set_error(errno, "");
887  return -1;
888  }
889  if (get_int_ary(mesh->adapt_type, mesh->n_elem_gross, fp)) {
890  return -1;
891  }
892 
893  /* adapt_level */
894  if ((mesh->adapt_level =
895  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
896  HECMW_set_error(errno, "");
897  return -1;
898  }
899  if (get_int_ary(mesh->adapt_level, mesh->n_elem_gross, fp)) {
900  return -1;
901  }
902 
903  /* adapt_parent */
904  if ((mesh->adapt_parent =
905  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross * 2)) == NULL) {
906  HECMW_set_error(errno, "");
907  return -1;
908  }
909  if (get_int_ary(mesh->adapt_parent, 2 * mesh->n_elem_gross, fp)) {
910  return -1;
911  }
912 
913  /* adapt_children_index */
914  if ((mesh->adapt_children_index = (int *)HECMW_malloc(
915  sizeof(int) * (mesh->n_elem_gross + 1))) == NULL) {
916  HECMW_set_error(errno, "");
917  return -1;
918  }
919  if (get_int_ary(mesh->adapt_children_index, mesh->n_elem_gross + 1, fp)) {
920  return -1;
921  }
922 
923  /* adapt_children_item */
924  if ((mesh->adapt_children_item = (int *)HECMW_malloc(
925  sizeof(int) * mesh->adapt_children_index[mesh->n_elem_gross] * 2)) ==
926  NULL) {
927  HECMW_set_error(errno, "");
928  return -1;
929  }
930  if (get_int_ary(mesh->adapt_children_item,
932  return -1;
933  }
934 
935  return 0;
936 }
937 
938 /*============================================================================*/
939 /* */
940 /* get section information */
941 /* */
942 /*============================================================================*/
943 static int get_section_info(struct hecmwST_section *sect, FILE *fp) {
944  /* n_sect */
945  if (get_int(&sect->n_sect, fp)) {
946  return -1;
947  }
948 
949  if (sect->n_sect == 0) {
950  sect->sect_type = NULL;
951  sect->sect_opt = NULL;
952  sect->sect_mat_ID_index = NULL;
953  sect->sect_mat_ID_item = NULL;
954  sect->sect_I_index = NULL;
955  sect->sect_I_item = NULL;
956  sect->sect_R_index = NULL;
957  sect->sect_R_item = NULL;
958  return 0;
959  }
960 
961  /* sect_type */
962  if ((sect->sect_type = (int *)HECMW_malloc(sizeof(int) * sect->n_sect)) ==
963  NULL) {
964  HECMW_set_error(errno, "");
965  return -1;
966  }
967  if (get_int_ary(sect->sect_type, sect->n_sect, fp)) {
968  return -1;
969  }
970 
971  /* sect_opt */
972  if ((sect->sect_opt = (int *)HECMW_malloc(sizeof(int) * sect->n_sect)) ==
973  NULL) {
974  HECMW_set_error(errno, "");
975  return -1;
976  }
977  if (get_int_ary(sect->sect_opt, sect->n_sect, fp)) {
978  return -1;
979  }
980 
981  /* sect_mat_ID_index */
982  if ((sect->sect_mat_ID_index =
983  (int *)HECMW_malloc(sizeof(int) * (sect->n_sect + 1))) == NULL) {
984  HECMW_set_error(errno, "");
985  return -1;
986  }
987  if (get_int_ary(sect->sect_mat_ID_index, sect->n_sect + 1, fp)) {
988  return -1;
989  }
990 
991  /* sect_mat_ID_item */
992  if (sect->sect_mat_ID_index[sect->n_sect] > 0) {
993  if ((sect->sect_mat_ID_item = (int *)HECMW_malloc(
994  sizeof(int) * sect->sect_mat_ID_index[sect->n_sect])) == NULL) {
995  HECMW_set_error(errno, "");
996  return -1;
997  }
998  if (get_int_ary(sect->sect_mat_ID_item,
999  sect->sect_mat_ID_index[sect->n_sect], fp)) {
1000  return -1;
1001  }
1002  }
1003 
1004  /* sect_I_index */
1005  if ((sect->sect_I_index =
1006  (int *)HECMW_malloc(sizeof(int) * (sect->n_sect + 1))) == NULL) {
1007  HECMW_set_error(errno, "");
1008  return -1;
1009  }
1010  if (get_int_ary(sect->sect_I_index, sect->n_sect + 1, fp)) {
1011  return -1;
1012  }
1013 
1014  /* sect_I_item */
1015  if (sect->sect_I_index[sect->n_sect] > 0) {
1016  if ((sect->sect_I_item = (int *)HECMW_malloc(
1017  sizeof(int) * sect->sect_I_index[sect->n_sect])) == NULL) {
1018  HECMW_set_error(errno, "");
1019  return -1;
1020  }
1021  if (get_int_ary(sect->sect_I_item, sect->sect_I_index[sect->n_sect], fp)) {
1022  return -1;
1023  }
1024  }
1025 
1026  /* sect_R_index */
1027  if ((sect->sect_R_index =
1028  (int *)HECMW_malloc(sizeof(int) * (sect->n_sect + 1))) == NULL) {
1029  HECMW_set_error(errno, "");
1030  return -1;
1031  }
1032  if (get_int_ary(sect->sect_R_index, sect->n_sect + 1, fp)) {
1033  return -1;
1034  }
1035 
1036  /* sect_R_item */
1037  if (sect->sect_R_index[sect->n_sect] > 0) {
1038  if ((sect->sect_R_item = (double *)HECMW_malloc(
1039  sizeof(double) * sect->sect_R_index[sect->n_sect])) == NULL) {
1040  HECMW_set_error(errno, "");
1041  return -1;
1042  }
1043  if (get_double_ary(sect->sect_R_item, sect->sect_R_index[sect->n_sect],
1044  fp)) {
1045  return -1;
1046  }
1047  }
1048 
1049  return 0;
1050 }
1051 
1052 /*============================================================================*/
1053 /* */
1054 /* get material information */
1055 /* */
1056 /*============================================================================*/
1057 static int get_material_info(struct hecmwST_material *mat, FILE *fp) {
1058  /* n_mat */
1059  if (get_int(&mat->n_mat, fp)) {
1060  return -1;
1061  }
1062 
1063  if (mat->n_mat == 0) {
1064  mat->n_mat_item = 0;
1065  mat->n_mat_subitem = 0;
1066  mat->n_mat_table = 0;
1067  mat->mat_name = NULL;
1068  mat->mat_item_index = NULL;
1069  mat->mat_subitem_index = NULL;
1070  mat->mat_table_index = NULL;
1071  mat->mat_val = NULL;
1072  mat->mat_temp = NULL;
1073  return 0;
1074  }
1075 
1076  /* n_mat_item */
1077  if (get_int(&mat->n_mat_item, fp)) {
1078  return -1;
1079  }
1080 
1081  /* n_mat_subitem */
1082  if (get_int(&mat->n_mat_subitem, fp)) {
1083  return -1;
1084  }
1085 
1086  /* n_mat_table */
1087  if (get_int(&mat->n_mat_table, fp)) {
1088  return -1;
1089  }
1090 
1091  /* mat_name */
1092  if ((mat->mat_name = (char **)HECMW_malloc(sizeof(char *) * mat->n_mat)) ==
1093  NULL) {
1094  HECMW_set_error(errno, "");
1095  return -1;
1096  }
1097  if (get_string_ary(mat->mat_name, mat->n_mat, fp)) {
1098  return -1;
1099  }
1100 
1101  /* mat_item_index */
1102  if ((mat->mat_item_index =
1103  (int *)HECMW_malloc(sizeof(int) * (mat->n_mat + 1))) == NULL) {
1104  HECMW_set_error(errno, "");
1105  return -1;
1106  }
1107  if (get_int_ary(mat->mat_item_index, mat->n_mat + 1, fp)) {
1108  return -1;
1109  }
1110 
1111  /* mat_subitem_index */
1112  if ((mat->mat_subitem_index =
1113  (int *)HECMW_malloc(sizeof(int) * (mat->n_mat_item + 1))) == NULL) {
1114  HECMW_set_error(errno, "");
1115  return -1;
1116  }
1117  if (get_int_ary(mat->mat_subitem_index, mat->n_mat_item + 1, fp)) {
1118  return -1;
1119  }
1120 
1121  /* mat_table_index */
1122  if ((mat->mat_table_index = (int *)HECMW_malloc(
1123  sizeof(int) * (mat->n_mat_subitem + 1))) == NULL) {
1124  HECMW_set_error(errno, "");
1125  return -1;
1126  }
1127  if (get_int_ary(mat->mat_table_index, mat->n_mat_subitem + 1, fp)) {
1128  return -1;
1129  }
1130 
1131  /* mat_val */
1132  if ((mat->mat_val =
1133  (double *)HECMW_malloc(sizeof(double) * mat->n_mat_table)) == NULL) {
1134  HECMW_set_error(errno, "");
1135  return -1;
1136  }
1137  if (get_double_ary(mat->mat_val, mat->n_mat_table, fp)) {
1138  return -1;
1139  }
1140 
1141  /* mat_temp */
1142  if ((mat->mat_temp =
1143  (double *)HECMW_malloc(sizeof(double) * mat->n_mat_table)) == NULL) {
1144  HECMW_set_error(errno, "");
1145  return -1;
1146  }
1147  if (get_double_ary(mat->mat_temp, mat->n_mat_table, fp)) {
1148  return -1;
1149  }
1150 
1151  return 0;
1152 }
1153 
1154 /*============================================================================*/
1155 /* */
1156 /* get MPC group information */
1157 /* */
1158 /*============================================================================*/
1159 static int get_mpc_info(struct hecmwST_mpc *mpc, FILE *fp,
1160  int hecmw_flag_version) {
1161  /* n_mpc */
1162  if (get_int(&mpc->n_mpc, fp)) {
1163  return -1;
1164  }
1165 
1166  if (mpc->n_mpc == 0) {
1167  mpc->mpc_item = NULL;
1168  mpc->mpc_dof = NULL;
1169  mpc->mpc_val = NULL;
1170  mpc->mpc_index = HECMW_malloc(sizeof(int));
1171  if (mpc->mpc_index == NULL) {
1172  HECMW_set_error(errno, "");
1173  return -1;
1174  }
1175  mpc->mpc_index[0] = 0;
1176  return 0;
1177  }
1178 
1179  /* mpc_index */
1180  if ((mpc->mpc_index = (int *)HECMW_malloc(sizeof(int) * (mpc->n_mpc + 1))) ==
1181  NULL) {
1182  HECMW_set_error(errno, "");
1183  return -1;
1184  }
1185  if (get_int_ary(mpc->mpc_index, mpc->n_mpc + 1, fp)) {
1186  return -1;
1187  }
1188 
1189  /* mpc_item */
1190  if ((mpc->mpc_item = (int *)HECMW_malloc(
1191  sizeof(int) * mpc->mpc_index[mpc->n_mpc])) == NULL) {
1192  HECMW_set_error(errno, "");
1193  return -1;
1194  }
1195  if (get_int_ary(mpc->mpc_item, mpc->mpc_index[mpc->n_mpc], fp)) {
1196  return -1;
1197  }
1198 
1199  /* mpc_dof */
1200  if ((mpc->mpc_dof = (int *)HECMW_malloc(
1201  sizeof(int) * mpc->mpc_index[mpc->n_mpc])) == NULL) {
1202  HECMW_set_error(errno, "");
1203  return -1;
1204  }
1205  if (get_int_ary(mpc->mpc_dof, mpc->mpc_index[mpc->n_mpc], fp)) {
1206  return -1;
1207  }
1208 
1209  /* mpc_val */
1210  if ((mpc->mpc_val = (double *)HECMW_malloc(
1211  sizeof(double) * mpc->mpc_index[mpc->n_mpc])) == NULL) {
1212  HECMW_set_error(errno, "");
1213  return -1;
1214  }
1215  if (get_double_ary(mpc->mpc_val, mpc->mpc_index[mpc->n_mpc], fp)) {
1216  return -1;
1217  }
1218 
1219  /* mpc_const */
1220  if ((mpc->mpc_const = (double *)HECMW_calloc(mpc->n_mpc, sizeof(double))) ==
1221  NULL) {
1222  HECMW_set_error(errno, "");
1223  return -1;
1224  }
1225  if (hecmw_flag_version >= 3) {
1226  if (get_double_ary(mpc->mpc_const, mpc->n_mpc, fp)) {
1227  return -1;
1228  }
1229  }
1230 
1231  return 0;
1232 }
1233 
1234 /*============================================================================*/
1235 /* */
1236 /* get amplitude information */
1237 /* */
1238 /*============================================================================*/
1239 static int get_amp_info(struct hecmwST_amplitude *amp, FILE *fp) {
1240  /* n_amp */
1241  if (get_int(&amp->n_amp, fp)) {
1242  return -1;
1243  }
1244 
1245  if (amp->n_amp == 0) {
1246  amp->amp_name = NULL;
1247  amp->amp_type_definition = NULL;
1248  amp->amp_type_time = NULL;
1249  amp->amp_type_value = NULL;
1250  amp->amp_val = NULL;
1251  amp->amp_table = NULL;
1252  amp->amp_index = HECMW_malloc(sizeof(int));
1253  if (amp->amp_index == NULL) {
1254  HECMW_set_error(errno, "");
1255  return -1;
1256  }
1257  amp->amp_index[0] = 0;
1258  return 0;
1259  }
1260 
1261  /* amp_name */
1262  if ((amp->amp_name = (char **)HECMW_malloc(sizeof(char *) * amp->n_amp)) ==
1263  NULL) {
1264  HECMW_set_error(errno, "");
1265  return -1;
1266  }
1267  if (get_string_ary(amp->amp_name, amp->n_amp, fp)) {
1268  return -1;
1269  }
1270 
1271  /* amp_type_definition */
1272  if ((amp->amp_type_definition =
1273  (int *)HECMW_malloc(sizeof(int) * amp->n_amp)) == NULL) {
1274  HECMW_set_error(errno, "");
1275  return -1;
1276  }
1277  if (get_int_ary(amp->amp_type_definition, amp->n_amp, fp)) {
1278  return -1;
1279  }
1280 
1281  /* amp_type_time */
1282  if ((amp->amp_type_time = (int *)HECMW_malloc(sizeof(int) * amp->n_amp)) ==
1283  NULL) {
1284  HECMW_set_error(errno, "");
1285  return -1;
1286  }
1287  if (get_int_ary(amp->amp_type_time, amp->n_amp, fp)) {
1288  return -1;
1289  }
1290 
1291  /* amp_type_value */
1292  if ((amp->amp_type_value = (int *)HECMW_malloc(sizeof(int) * amp->n_amp)) ==
1293  NULL) {
1294  HECMW_set_error(errno, "");
1295  return -1;
1296  }
1297  if (get_int_ary(amp->amp_type_value, amp->n_amp, fp)) {
1298  return -1;
1299  }
1300 
1301  /* amp_index */
1302  if ((amp->amp_index = (int *)HECMW_malloc(sizeof(int) * (amp->n_amp + 1))) ==
1303  NULL) {
1304  HECMW_set_error(errno, "");
1305  return -1;
1306  }
1307  if (get_int_ary(amp->amp_index, amp->n_amp + 1, fp)) {
1308  return -1;
1309  }
1310 
1311  /* amp_val */
1312  if ((amp->amp_val = (double *)HECMW_malloc(
1313  sizeof(double) * amp->amp_index[amp->n_amp])) == NULL) {
1314  HECMW_set_error(errno, "");
1315  return -1;
1316  }
1317  if (get_double_ary(amp->amp_val, amp->amp_index[amp->n_amp], fp)) {
1318  return -1;
1319  }
1320 
1321  /* amp_table */
1322  if ((amp->amp_table = (double *)HECMW_malloc(
1323  sizeof(double) * amp->amp_index[amp->n_amp])) == NULL) {
1324  HECMW_set_error(errno, "");
1325  return -1;
1326  }
1327  if (get_double_ary(amp->amp_table, amp->amp_index[amp->n_amp], fp)) {
1328  return -1;
1329  }
1330 
1331  return 0;
1332 }
1333 
1334 /*============================================================================*/
1335 /* */
1336 /* get node group information */
1337 /* */
1338 /*============================================================================*/
1339 static int get_node_group_info(struct hecmwST_node_grp *grp, FILE *fp) {
1340  /* n_grp */
1341  if (get_int(&grp->n_grp, fp)) {
1342  return -1;
1343  }
1344 
1345  if (grp->n_grp == 0) {
1346  grp->grp_name = NULL;
1347  grp->grp_item = NULL;
1348  grp->grp_index = HECMW_malloc(sizeof(int));
1349  if (grp->grp_index == NULL) {
1350  HECMW_set_error(errno, "");
1351  return -1;
1352  }
1353  grp->grp_index[0] = 0;
1354  return 0;
1355  }
1356 
1357  /* grp_name */
1358  if ((grp->grp_name = (char **)HECMW_malloc(sizeof(char *) * grp->n_grp)) ==
1359  NULL) {
1360  HECMW_set_error(errno, "");
1361  return -1;
1362  }
1363  if (get_string_ary(grp->grp_name, grp->n_grp, fp)) {
1364  return -1;
1365  }
1366 
1367  /* grp_index */
1368  if ((grp->grp_index = (int *)HECMW_malloc(sizeof(int) * (grp->n_grp + 1))) ==
1369  NULL) {
1370  HECMW_set_error(errno, "");
1371  return -1;
1372  }
1373  if (get_int_ary(grp->grp_index, grp->n_grp + 1, fp)) {
1374  return -1;
1375  }
1376 
1377  /* grp_item */
1378  if (grp->grp_index[grp->n_grp] > 0) {
1379  if ((grp->grp_item = (int *)HECMW_malloc(
1380  sizeof(int) * grp->grp_index[grp->n_grp])) == NULL) {
1381  HECMW_set_error(errno, "");
1382  return -1;
1383  }
1384  if (get_int_ary(grp->grp_item, grp->grp_index[grp->n_grp], fp)) {
1385  return -1;
1386  }
1387  } else {
1388  grp->grp_item = NULL;
1389  }
1390 
1391  return 0;
1392 }
1393 
1394 /*============================================================================*/
1395 /* */
1396 /* get element group information */
1397 /* */
1398 /*============================================================================*/
1399 static int get_elem_group_info(struct hecmwST_elem_grp *grp, FILE *fp) {
1400  /* n_grp */
1401  if (get_int(&grp->n_grp, fp)) {
1402  return -1;
1403  }
1404 
1405  if (grp->n_grp == 0) {
1406  grp->grp_name = NULL;
1407  grp->grp_item = NULL;
1408  grp->grp_index = HECMW_malloc(sizeof(int));
1409  if (grp->grp_index == NULL) {
1410  HECMW_set_error(errno, "");
1411  return -1;
1412  }
1413  grp->grp_index[0] = 0;
1414  return 0;
1415  }
1416 
1417  /* grp_name */
1418  if ((grp->grp_name = (char **)HECMW_malloc(sizeof(char *) * grp->n_grp)) ==
1419  NULL) {
1420  HECMW_set_error(errno, "");
1421  return -1;
1422  }
1423  if (get_string_ary(grp->grp_name, grp->n_grp, fp)) {
1424  return -1;
1425  }
1426 
1427  /* grp_index */
1428  if ((grp->grp_index = (int *)HECMW_malloc(sizeof(int) * (grp->n_grp + 1))) ==
1429  NULL) {
1430  HECMW_set_error(errno, "");
1431  return -1;
1432  }
1433  if (get_int_ary(grp->grp_index, grp->n_grp + 1, fp)) {
1434  return -1;
1435  }
1436 
1437  /* grp_item */
1438  if (grp->grp_index[grp->n_grp] > 0) {
1439  if ((grp->grp_item = (int *)HECMW_malloc(
1440  sizeof(int) * grp->grp_index[grp->n_grp])) == NULL) {
1441  HECMW_set_error(errno, "");
1442  return -1;
1443  }
1444  if (get_int_ary(grp->grp_item, grp->grp_index[grp->n_grp], fp)) {
1445  return -1;
1446  }
1447  } else {
1448  grp->grp_item = NULL;
1449  }
1450 
1451  return 0;
1452 }
1453 
1454 /*============================================================================*/
1455 /* */
1456 /* get surface group information */
1457 /* */
1458 /*============================================================================*/
1459 static int get_surf_group_info(struct hecmwST_surf_grp *grp, FILE *fp) {
1460  /* n_grp */
1461  if (get_int(&grp->n_grp, fp)) {
1462  return -1;
1463  }
1464 
1465  if (grp->n_grp == 0) {
1466  grp->grp_name = NULL;
1467  grp->grp_item = NULL;
1468  grp->grp_index = HECMW_malloc(sizeof(int));
1469  if (grp->grp_index == NULL) {
1470  HECMW_set_error(errno, "");
1471  return -1;
1472  }
1473  grp->grp_index[0] = 0;
1474  return 0;
1475  }
1476 
1477  /* grp_name */
1478  if ((grp->grp_name = (char **)HECMW_malloc(sizeof(char *) * grp->n_grp)) ==
1479  NULL) {
1480  HECMW_set_error(errno, "");
1481  return -1;
1482  }
1483  if (get_string_ary(grp->grp_name, grp->n_grp, fp)) {
1484  return -1;
1485  }
1486 
1487  /* grp_index */
1488  if ((grp->grp_index = (int *)HECMW_malloc(sizeof(int) * (grp->n_grp + 1))) ==
1489  NULL) {
1490  HECMW_set_error(errno, "");
1491  return -1;
1492  }
1493  if (get_int_ary(grp->grp_index, grp->n_grp + 1, fp)) {
1494  return -1;
1495  }
1496 
1497  /* grp_item */
1498  if (grp->grp_index[grp->n_grp] > 0) {
1499  if ((grp->grp_item = (int *)HECMW_malloc(
1500  sizeof(int) * grp->grp_index[grp->n_grp] * 2)) == NULL) {
1501  HECMW_set_error(errno, "");
1502  return -1;
1503  }
1504  if (get_int_ary(grp->grp_item, grp->grp_index[grp->n_grp] * 2, fp)) {
1505  return -1;
1506  }
1507  } else {
1508  grp->grp_item = NULL;
1509  }
1510 
1511  return 0;
1512 }
1513 
1514 /*============================================================================*/
1515 /* */
1516 /* get contact information */
1517 /* */
1518 /*============================================================================*/
1519 static int get_contact_info(struct hecmwST_contact_pair *cpair, FILE *fp,
1520  int hecmw_flag_version) {
1521  if (hecmw_flag_version < 3) {
1522  cpair->n_pair = 0;
1523  cpair->type = NULL;
1524  cpair->slave_grp_id = NULL;
1525  cpair->slave_orisgrp_id = NULL;
1526  cpair->master_grp_id = NULL;
1527  return 0;
1528  }
1529 
1530  /* n_pair */
1531  if (get_int(&cpair->n_pair, fp)) {
1532  return -1;
1533  }
1534 
1535  if (cpair->n_pair == 0) {
1536  cpair->name = NULL;
1537  cpair->type = NULL;
1538  cpair->slave_grp_id = NULL;
1539  cpair->slave_orisgrp_id = NULL;
1540  cpair->master_grp_id = NULL;
1541  return 0;
1542  }
1543 
1544  /* name */
1545  if ((cpair->name = (char **)HECMW_malloc(sizeof(char *) * cpair->n_pair)) ==
1546  NULL) {
1547  HECMW_set_error(errno, "");
1548  return -1;
1549  }
1550  if (get_string_ary(cpair->name, cpair->n_pair, fp)) {
1551  return -1;
1552  }
1553 
1554  /* type */
1555  if ((cpair->type = (int *)HECMW_malloc(sizeof(int) * (cpair->n_pair))) ==
1556  NULL) {
1557  HECMW_set_error(errno, "");
1558  return -1;
1559  }
1560  if (get_int_ary(cpair->type, cpair->n_pair, fp)) {
1561  return -1;
1562  }
1563 
1564  /* slave_grp_id */
1565  if ((cpair->slave_grp_id =
1566  (int *)HECMW_malloc(sizeof(int) * (cpair->n_pair))) == NULL) {
1567  HECMW_set_error(errno, "");
1568  return -1;
1569  }
1570  if (get_int_ary(cpair->slave_grp_id, cpair->n_pair, fp)) {
1571  return -1;
1572  }
1573 
1574  /* slave_orisgrp_id */
1575  if ((cpair->slave_orisgrp_id =
1576  (int *)HECMW_malloc(sizeof(int) * (cpair->n_pair))) == NULL) {
1577  HECMW_set_error(errno, "");
1578  return -1;
1579  }
1580  if (hecmw_flag_version >= 5) {
1581  if (get_int_ary(cpair->slave_orisgrp_id, cpair->n_pair, fp)) {
1582  return -1;
1583  }
1584  }
1585 
1586  /* master_grp_id */
1587  if ((cpair->master_grp_id =
1588  (int *)HECMW_malloc(sizeof(int) * (cpair->n_pair))) == NULL) {
1589  HECMW_set_error(errno, "");
1590  return -1;
1591  }
1592  if (get_int_ary(cpair->master_grp_id, cpair->n_pair, fp)) {
1593  return -1;
1594  }
1595 
1596  return 0;
1597 }
1598 
1599 /*============================================================================*/
1600 /* */
1601 /* get refinement information */
1602 /* */
1603 /*============================================================================*/
1604 static int get_refine_info(struct hecmwST_local_mesh *mesh, FILE *fp) {
1605  if (mesh->hecmw_flag_version < 2) {
1606  mesh->n_refine = 0;
1607  mesh->node_old2new = NULL;
1608  mesh->node_new2old = NULL;
1609  return 0;
1610  }
1611 
1612  /* number of refinement performed */
1613  if (get_int(&mesh->n_refine, fp)) {
1614  return -1;
1615  }
1616  if (mesh->n_refine == 0 || mesh->n_subdomain == 1) {
1617  mesh->node_old2new = NULL;
1618  mesh->node_new2old = NULL;
1619  return 0;
1620  }
1621 
1622  if (mesh->n_node_gross > mesh->nn_internal) {
1623  /* node_old2new */
1624  if ((mesh->node_old2new =
1625  (int *)HECMW_malloc(sizeof(int) * mesh->n_node_gross)) == NULL) {
1626  HECMW_set_error(errno, "");
1627  return -1;
1628  }
1629  if (get_int_ary(mesh->node_old2new, mesh->n_node_gross, fp)) {
1630  return -1;
1631  }
1632 
1633  /* node_new2old */
1634  if ((mesh->node_new2old =
1635  (int *)HECMW_malloc(sizeof(int) * mesh->n_node_gross)) == NULL) {
1636  HECMW_set_error(errno, "");
1637  return -1;
1638  }
1639  if (get_int_ary(mesh->node_new2old, mesh->n_node_gross, fp)) {
1640  return -1;
1641  }
1642  }
1643 
1644  if (mesh->n_elem_gross > mesh->n_elem) {
1645  /* elem_old2new */
1646  if ((mesh->elem_old2new =
1647  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
1648  HECMW_set_error(errno, "");
1649  return -1;
1650  }
1651  if (get_int_ary(mesh->elem_old2new, mesh->n_elem_gross, fp)) {
1652  return -1;
1653  }
1654 
1655  /* elem_new2old */
1656  if ((mesh->elem_new2old =
1657  (int *)HECMW_malloc(sizeof(int) * mesh->n_elem_gross)) == NULL) {
1658  HECMW_set_error(errno, "");
1659  return -1;
1660  }
1661  if (get_int_ary(mesh->elem_new2old, mesh->n_elem_gross, fp)) {
1662  return -1;
1663  }
1664  }
1665 
1666  return 0;
1667 }
1668 
1669 static void set_comm(struct hecmwST_local_mesh *mesh) {
1672  mesh->PEsmpTOT = 1;
1674  if (mesh->my_rank == 0) {
1675  mesh->zero = 1;
1676  } else {
1677  mesh->zero = 0;
1678  }
1679 }
1680 
1681 /*============================================================================*/
1682 /* */
1683 /* get HEC-MW distributed mesh format data */
1684 /* */
1685 /*============================================================================*/
1686 extern struct hecmwST_local_mesh *HECMW_get_dist_mesh(char *fname) {
1687  FILE *fp;
1688  struct hecmwST_local_mesh *mesh;
1689 
1690  HECMW_log(HECMW_LOG_DEBUG, "Start to read HECW-DIST file");
1691 
1692  /* allocation */
1693  if ((mesh = HECMW_dist_alloc()) == NULL) {
1694  return NULL;
1695  }
1696 
1697  /* file open */
1698  if ((fp = fopen(fname, "r")) == NULL) {
1699  HECMW_set_error(HECMW_IO_E5001, "File: %s, %s", fname, HECMW_strmsg(errno));
1700  return NULL;
1701  }
1702 
1703  /* check file type */
1704  if (!is_hecmw_dist_file(fp)) {
1705  return NULL;
1706  }
1707 
1708  /* rewind */
1709  if (rewind_fp(fp)) {
1710  return NULL;
1711  }
1712 
1713  /* get data */
1714  if (get_header(fp)) {
1715  return NULL;
1716  }
1717 
1718  if (get_global_info(mesh, fp)) {
1719  return NULL;
1720  }
1721 
1722  if (get_node_info(mesh, fp)) {
1723  return NULL;
1724  }
1725 
1726  if (get_elem_info(mesh, fp)) {
1727  return NULL;
1728  }
1729 
1730  if (get_comm_info(mesh, fp)) {
1731  return NULL;
1732  }
1733 
1734  if (get_adapt_info(mesh, fp)) {
1735  return NULL;
1736  }
1737 
1738  if (get_section_info(mesh->section, fp)) {
1739  return NULL;
1740  }
1741 
1742  if (get_material_info(mesh->material, fp)) {
1743  return NULL;
1744  }
1745 
1746  if (get_mpc_info(mesh->mpc, fp, mesh->hecmw_flag_version)) {
1747  return NULL;
1748  }
1749 
1750  if (get_amp_info(mesh->amp, fp)) {
1751  return NULL;
1752  }
1753 
1754  if (get_node_group_info(mesh->node_group, fp)) {
1755  return NULL;
1756  }
1757 
1758  if (get_elem_group_info(mesh->elem_group, fp)) {
1759  return NULL;
1760  }
1761 
1762  if (get_surf_group_info(mesh->surf_group, fp)) {
1763  return NULL;
1764  }
1765 
1766  if (get_refine_info(mesh, fp)) {
1767  return NULL;
1768  }
1769 
1770  if (get_contact_info(mesh->contact_pair, fp, mesh->hecmw_flag_version)) {
1771  return NULL;
1772  }
1773 
1774  /* close file */
1775  if (fclose(fp)) {
1777  return NULL;
1778  }
1779 
1780  set_comm(mesh);
1781 
1782  if (mesh->hecmw_flag_version < 4) {
1783  mesh->hecmw_flag_version = 4;
1784  }
1785 
1786  return mesh;
1787 }
1788 
1789 /* */
1790 /* print data */
1791 /* */
1792 
1793 /*----------------------------------------------------------------------------*/
1794 /* print int */
1795 /*----------------------------------------------------------------------------*/
1796 static int print_int(int item, FILE *fp) {
1797  int rtc;
1798 
1799  rtc = fprintf(fp, "%d\n", item);
1800  if (rtc < 0) {
1802  return -1;
1803  }
1804 
1805  return 0;
1806 }
1807 
1808 /*----------------------------------------------------------------------------*/
1809 /* print double */
1810 /*----------------------------------------------------------------------------*/
1811 static int print_double(double item, FILE *fp) {
1812  int rtc;
1813 
1814  rtc = fprintf(fp, "%.16E\n", item);
1815  if (rtc < 0) {
1817  return -1;
1818  }
1819 
1820  return 0;
1821 }
1822 
1823 /*----------------------------------------------------------------------------*/
1824 /* print string */
1825 /*----------------------------------------------------------------------------*/
1826 static int print_string(const char *item, FILE *fp) {
1827  int rtc;
1828 
1829  rtc = fprintf(fp, "%s\n", item);
1830  if (rtc < 0) {
1832  return -1;
1833  }
1834 
1835  return 0;
1836 }
1837 
1838 /*----------------------------------------------------------------------------*/
1839 /* print HECMW_Comm */
1840 /*----------------------------------------------------------------------------*/
1841 static int print_comm(HECMW_Comm item, FILE *fp) {
1842  int rtc;
1843 
1844  /* rtc = fprintf( fp, "%d\n", (int)item ); */
1845  rtc = fprintf(fp, "%d\n", 0);
1846  if (rtc < 0) {
1848  return -1;
1849  }
1850 
1851  return 0;
1852 }
1853 
1854 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1855 /* print int (array) */
1856 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1857 static int print_int_ary(const int *ary, int n, int cols, FILE *fp) {
1858  int rtc, i;
1859 
1860  if (n <= 0) return 0;
1861 
1862  for (i = 0; i < n; i++) {
1863  rtc = fprintf(fp, "%d%c", ary[i], (i + 1) % cols ? ' ' : '\n');
1864  if (rtc < 0) {
1866  return -1;
1867  }
1868  }
1869  if (n % cols) {
1870  rtc = fprintf(fp, "\n");
1871  if (rtc < 0) {
1873  return -1;
1874  }
1875  }
1876 
1877  return 0;
1878 }
1879 
1880 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1881 /* Generic: print long long array as int array with overflow check */
1882 /* (for distributed mesh file compatibility) */
1883 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1884 static int print_longlong_as_int_ary(const long long *ary, size_t n, int cols,
1885  FILE *fp, const char *name) {
1886  int rtc;
1887  size_t i;
1888 
1889  if (n <= 0) return 0;
1890 
1891  for (i = 0; i < n; i++) {
1892  /* Overflow check */
1893  if (ary[i] > INT_MAX || ary[i] < INT_MIN) {
1895  "%s[%zu]=%lld exceeds INT range after partitioning",
1896  name ? name : "array", i, ary[i]);
1897  return -1;
1898  }
1899 
1900  rtc = fprintf(fp, "%d%c", (int)ary[i], ((int)(i + 1)) % cols ? ' ' : '\n');
1901  if (rtc < 0) {
1903  return -1;
1904  }
1905  }
1906  if (n % cols) {
1907  rtc = fprintf(fp, "\n");
1908  if (rtc < 0) {
1910  return -1;
1911  }
1912  }
1913 
1914  return 0;
1915 }
1916 
1917 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1918 /* print double (array) */
1919 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1920 static int print_double_ary(const double *ary, int n, int cols, FILE *fp) {
1921  int rtc, i;
1922 
1923  if (n <= 0) return 0;
1924 
1925  for (i = 0; i < n; i++) {
1926  rtc = fprintf(fp, "%.16E%c", ary[i], (i + 1) % cols ? ' ' : '\n');
1927  if (rtc < 0) {
1929  return -1;
1930  }
1931  }
1932  if (n % cols) {
1933  rtc = fprintf(fp, "\n");
1934  if (rtc < 0) {
1936  return -1;
1937  }
1938  }
1939 
1940  return 0;
1941 }
1942 
1943 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1944 /* print string (array) */
1945 /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1946 static int print_string_ary(char **ary, int n, FILE *fp) {
1947  int rtc, i;
1948 
1949  for (i = 0; i < n; i++) {
1950  rtc = fprintf(fp, "%s\n", ary[i]);
1951  if (rtc < 0) {
1953  return -1;
1954  }
1955  }
1956 
1957  return 0;
1958 }
1959 
1960 /*----------------------------------------------------------------------------*/
1961 /* print header */
1962 /*----------------------------------------------------------------------------*/
1963 static int print_header(const struct hecmwST_local_mesh *mesh, FILE *fp) {
1964  int rtc;
1965  char header[HECMW_HEADER_LEN + 1];
1966 
1967  strcpy(header, HEADER_STRING);
1968  {
1969  size_t len = strlen(header);
1970  rtc = snprintf(header + len, sizeof(header) - len, "%d", mesh->hecmw_flag_version);
1971  }
1972  if (rtc < 0) {
1974  return -1;
1975  }
1976 
1977  if (print_string(header, fp)) {
1978  return -1;
1979  } else {
1980  return 0;
1981  }
1982 }
1983 
1984 /*----------------------------------------------------------------------------*/
1985 /* print global information */
1986 /*----------------------------------------------------------------------------*/
1987 static int print_global_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
1988  int flag_header;
1989 
1990  /* hecmw_flag_adapt */
1991  if (print_int(mesh->hecmw_flag_adapt, fp)) {
1992  return -1;
1993  }
1994 
1995  /* hecmw_flag_initcon */
1996  if (print_int(mesh->hecmw_flag_initcon, fp)) {
1997  return -1;
1998  }
1999 
2000  /* hecmw_flag_parttype */
2001  if (print_int(mesh->hecmw_flag_parttype, fp)) {
2002  return -1;
2003  }
2004 
2005  /* hecmw_flag_partdepth */
2006  if (print_int(mesh->hecmw_flag_partdepth, fp)) {
2007  return -1;
2008  }
2009 
2010  /* hecmw_flag_version */
2011  if (print_int(mesh->hecmw_flag_version, fp)) {
2012  return -1;
2013  }
2014 
2015  /* hecmw_flag_partcontact */
2016  if (print_int(mesh->hecmw_flag_partcontact, fp)) {
2017  return -1;
2018  }
2019 
2020  /* gridfile */
2021  if (print_string(mesh->gridfile, fp)) {
2022  return -1;
2023  }
2024 
2025  /* hecmw_n_files */
2026  if (print_int(mesh->hecmw_n_file, fp)) {
2027  return -1;
2028  }
2029 
2030  /* files */
2031  if (mesh->hecmw_n_file > 0) {
2032  if (print_string_ary(mesh->files, mesh->hecmw_n_file, fp)) {
2033  return -1;
2034  }
2035  }
2036 
2037  if (strlen(mesh->header) > 0) {
2038  /* flag for header */
2039  flag_header = 1;
2040  if (print_int(flag_header, fp)) {
2041  return -1;
2042  }
2043 
2044  /* header */
2045  if (print_string(mesh->header, fp)) {
2046  return -1;
2047  }
2048  } else {
2049  /* flag for header */
2050  flag_header = 0;
2051  if (print_int(flag_header, fp)) {
2052  return -1;
2053  }
2054  }
2055 
2056  /* zero_temp */
2057  if (print_double(mesh->zero_temp, fp)) {
2058  return -1;
2059  }
2060 
2061  return 0;
2062 }
2063 
2064 /*============================================================================*/
2065 /* */
2066 /* print node information */
2067 /* */
2068 /*============================================================================*/
2069 static int print_node_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2070  /* n_node */
2071  if (print_int(mesh->n_node, fp)) {
2072  return -1;
2073  }
2074 
2075  /* n_node_gross */
2076  if (print_int(mesh->n_node_gross, fp)) {
2077  return -1;
2078  }
2079 
2080  /* nn_middle */
2081  if (print_int(mesh->nn_middle, fp)) {
2082  return -1;
2083  }
2084 
2085  /* nn_internal */
2086  if (print_int(mesh->nn_internal, fp)) {
2087  return -1;
2088  }
2089 
2090  /* node_internal_list */
2093  if (print_int_ary(mesh->node_internal_list, mesh->nn_internal, COLS_INT_DEF,
2094  fp)) {
2095  return -1;
2096  }
2097  }
2098 
2099  /* node_ID */
2100  if (print_int_ary(mesh->node_ID, 2 * mesh->n_node_gross, COLS_TWO, fp)) {
2101  return -1;
2102  }
2103 
2104  /* global_node_ID */
2105  if (print_int_ary(mesh->global_node_ID, mesh->n_node_gross, COLS_INT_DEF,
2106  fp)) {
2107  return -1;
2108  }
2109 
2110  /* node */
2111  if (print_double_ary(mesh->node, 3 * mesh->n_node_gross, COLS_THREE, fp)) {
2112  return -1;
2113  }
2114 
2115  /* n_dof */
2116  if (print_int(mesh->n_dof, fp)) {
2117  return -1;
2118  }
2119 
2120  /* n_dof_grp */
2121  if (print_int(mesh->n_dof_grp, fp)) {
2122  return -1;
2123  }
2124 
2125  /* node_dof_index */
2126  if (print_int_ary(mesh->node_dof_index, mesh->n_dof_grp + 1, COLS_INT_DEF,
2127  fp)) {
2128  return -1;
2129  }
2130 
2131  /* node_dof_item */
2132  if (print_int_ary(mesh->node_dof_item, mesh->n_dof_grp, COLS_INT_DEF, fp)) {
2133  return -1;
2134  }
2135 
2136  if (mesh->hecmw_flag_initcon) {
2137  /* node_init_val_index */
2138  if (print_int_ary(mesh->node_init_val_index, mesh->n_node_gross + 1,
2139  COLS_INT_DEF, fp)) {
2140  return -1;
2141  }
2142 
2143  /* node_init_val_item */
2144  if (print_double_ary(mesh->node_init_val_item,
2146  COLS_DOUBLE_DEF, fp)) {
2147  return -1;
2148  }
2149  }
2150 
2151  /* node_val_index */
2152  /* node_val_item */
2153 
2154  return 0;
2155 }
2156 
2157 /*============================================================================*/
2158 /* */
2159 /* print element information */
2160 /* */
2161 /*============================================================================*/
2162 static int print_elem_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2163  /* n_elem */
2164  if (print_int(mesh->n_elem, fp)) {
2165  return -1;
2166  }
2167 
2168  /* n_elem_gross */
2169  if (print_int(mesh->n_elem_gross, fp)) {
2170  return -1;
2171  }
2172 
2173  /* ne_internal */
2174  if (print_int(mesh->ne_internal, fp)) {
2175  return -1;
2176  }
2177 
2178  /* elem_internal_list */
2181  if (print_int_ary(mesh->elem_internal_list, mesh->ne_internal, COLS_INT_DEF,
2182  fp)) {
2183  return -1;
2184  }
2185  }
2186 
2187  /* elem_ID */
2188  if (print_int_ary(mesh->elem_ID, 2 * mesh->n_elem_gross, COLS_TWO, fp)) {
2189  return -1;
2190  }
2191 
2192  /* global_elem_ID */
2193  if (print_int_ary(mesh->global_elem_ID, mesh->n_elem_gross, COLS_INT_DEF,
2194  fp)) {
2195  return -1;
2196  }
2197 
2198  /* elem_type */
2199  if (print_int_ary(mesh->elem_type, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2200  return -1;
2201  }
2202 
2203  /* n_elem_type */
2204  if (print_int(mesh->n_elem_type, fp)) {
2205  return -1;
2206  }
2207 
2208  /* elem_type_index */
2209  if (print_int_ary(mesh->elem_type_index, mesh->n_elem_type + 1, COLS_INT_DEF,
2210  fp)) {
2211  return -1;
2212  }
2213 
2214  /* elem_type_item */
2215  if (print_int_ary(mesh->elem_type_item, mesh->n_elem_type, COLS_INT_DEF,
2216  fp)) {
2217  return -1;
2218  }
2219 
2220  /* elem_node_index */
2221  if (print_longlong_as_int_ary(mesh->elem_node_index, mesh->n_elem_gross + 1,
2222  COLS_INT_DEF, fp, "elem_node_index")) {
2223  return -1;
2224  }
2225 
2226  /* elem_node_item */
2227  if (print_int_ary(mesh->elem_node_item,
2229  fp)) {
2230  return -1;
2231  }
2232 
2233  /* section_ID */
2234  if (print_int_ary(mesh->section_ID, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2235  return -1;
2236  }
2237 
2238  /* elem_mat_ID_index */
2239  if (print_int_ary(mesh->elem_mat_ID_index, mesh->n_elem_gross + 1,
2240  COLS_INT_DEF, fp)) {
2241  return -1;
2242  }
2243 
2244  /* elem_mat_ID_item */
2245  if (print_int_ary(mesh->elem_mat_ID_item,
2247  fp)) {
2248  return -1;
2249  }
2250 
2251  /* n_elem_mat_ID */
2252  if (print_int(mesh->n_elem_mat_ID, fp)) {
2253  return -1;
2254  }
2255 
2256  /* elem_mat_int_index */
2257  /* elem_mat_int_item */
2258 
2259  /* elem_val_index */
2260  /* elem_val_item */
2261 
2262  return 0;
2263 }
2264 
2265 /*============================================================================*/
2266 /* */
2267 /* print domain information & communication tables */
2268 /* */
2269 /*============================================================================*/
2270 static int print_comm_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2271  /* zero */
2272  if (print_int(mesh->zero, fp)) {
2273  return -1;
2274  }
2275 
2276  /* HECMW_COMM */
2277  if (print_comm(mesh->HECMW_COMM, fp)) {
2278  return -1;
2279  }
2280 
2281  /* PETOT */
2282  if (print_int(mesh->PETOT, fp)) {
2283  return -1;
2284  }
2285 
2286  /* PEsmpTOT */
2287  if (print_int(mesh->PEsmpTOT, fp)) {
2288  return -1;
2289  }
2290 
2291  /* my_rank */
2292  if (print_int(mesh->my_rank, fp)) {
2293  return -1;
2294  }
2295 
2296  /* errnof */
2297  if (print_int(mesh->errnof, fp)) {
2298  return -1;
2299  }
2300 
2301  /* n_subdomain */
2302  if (print_int(mesh->n_subdomain, fp)) {
2303  return -1;
2304  }
2305 
2306  /* n_neighbor_pd */
2307  if (print_int(mesh->n_neighbor_pe, fp)) {
2308  return -1;
2309  }
2310 
2311  if (mesh->n_neighbor_pe == 0) return 0;
2312 
2313  /* neighbor_pe */
2314  if (print_int_ary(mesh->neighbor_pe, mesh->n_neighbor_pe, COLS_INT_DEF, fp)) {
2315  return -1;
2316  }
2317 
2318  /* import_index */
2319  if (print_int_ary(mesh->import_index, mesh->n_neighbor_pe + 1, COLS_INT_DEF,
2320  fp)) {
2321  return -1;
2322  }
2323 
2324  /* import_item */
2325  if (print_int_ary(mesh->import_item, mesh->import_index[mesh->n_neighbor_pe],
2326  COLS_INT_DEF, fp)) {
2327  return -1;
2328  }
2329 
2330  /* export_index */
2331  if (print_int_ary(mesh->export_index, mesh->n_neighbor_pe + 1, COLS_INT_DEF,
2332  fp)) {
2333  return -1;
2334  }
2335 
2336  /* export_item */
2337  if (print_int_ary(mesh->export_item, mesh->export_index[mesh->n_neighbor_pe],
2338  COLS_INT_DEF, fp)) {
2339  return -1;
2340  }
2341 
2342  /* shared_index */
2343  if (print_int_ary(mesh->shared_index, mesh->n_neighbor_pe + 1, COLS_INT_DEF,
2344  fp)) {
2345  return -1;
2346  }
2347 
2348  /* shared_item */
2349  if (print_int_ary(mesh->shared_item, mesh->shared_index[mesh->n_neighbor_pe],
2350  COLS_INT_DEF, fp)) {
2351  return -1;
2352  }
2353 
2354  return 0;
2355 }
2356 
2357 /*============================================================================*/
2358 /* */
2359 /* print adaptation information */
2360 /* */
2361 /*============================================================================*/
2362 static int print_adapt_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2363  if (mesh->hecmw_flag_adapt == 0) return 0;
2364 
2365  /* coarse_grid_level */
2366  if (print_int(mesh->coarse_grid_level, fp)) {
2367  return -1;
2368  }
2369 
2370  /* n_adapt */
2371  if (print_int(mesh->n_adapt, fp)) {
2372  return -1;
2373  }
2374 
2375  /* when_i_was_refined_node */
2376  if (print_int_ary(mesh->when_i_was_refined_node, mesh->n_node_gross,
2377  COLS_INT_DEF, fp)) {
2378  return -1;
2379  }
2380 
2381  /* when_i_was_refined_elem */
2382  if (print_int_ary(mesh->when_i_was_refined_elem, mesh->n_elem_gross,
2383  COLS_INT_DEF, fp)) {
2384  return -1;
2385  }
2386 
2387  /* adapt_parent_type */
2388  if (print_int_ary(mesh->adapt_parent_type, mesh->n_elem_gross, COLS_INT_DEF,
2389  fp)) {
2390  return -1;
2391  }
2392 
2393  /* adapt_type */
2394  if (print_int_ary(mesh->adapt_type, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2395  return -1;
2396  }
2397 
2398  /* adapt_level */
2399  if (print_int_ary(mesh->adapt_level, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2400  return -1;
2401  }
2402 
2403  /* adapt_parent */
2404  if (print_int_ary(mesh->adapt_parent, 2 * mesh->n_elem_gross, COLS_TWO, fp)) {
2405  return -1;
2406  }
2407 
2408  /* adapt_children_index */
2409  if (print_int_ary(mesh->adapt_children_index, mesh->n_elem_gross + 1,
2410  COLS_INT_DEF, fp)) {
2411  return -1;
2412  }
2413 
2414  /* adapt_children_item */
2415  if (print_int_ary(mesh->adapt_children_item,
2417  COLS_TWO, fp)) {
2418  return -1;
2419  }
2420 
2421  return 0;
2422 }
2423 
2424 /*============================================================================*/
2425 /* */
2426 /* print section information */
2427 /* */
2428 /*============================================================================*/
2429 static int print_section_info(const struct hecmwST_section *sect, FILE *fp) {
2430  /* n_sect */
2431  if (print_int(sect->n_sect, fp)) {
2432  return -1;
2433  }
2434 
2435  if (sect->n_sect == 0) return 0;
2436 
2437  /* sect_type */
2438  if (print_int_ary(sect->sect_type, sect->n_sect, COLS_INT_DEF, fp)) {
2439  return -1;
2440  }
2441 
2442  /* sect_opt */
2443  if (print_int_ary(sect->sect_opt, sect->n_sect, COLS_INT_DEF, fp)) {
2444  return -1;
2445  }
2446 
2447  /* sect_mat_ID_index */
2448  if (print_int_ary(sect->sect_mat_ID_index, sect->n_sect + 1, COLS_INT_DEF,
2449  fp)) {
2450  return -1;
2451  }
2452 
2453  /* sect_mat_ID_item */
2454  if (print_int_ary(sect->sect_mat_ID_item,
2455  sect->sect_mat_ID_index[sect->n_sect], COLS_INT_DEF, fp)) {
2456  return -1;
2457  }
2458 
2459  /* sect_I_index */
2460  if (print_int_ary(sect->sect_I_index, sect->n_sect + 1, COLS_INT_DEF, fp)) {
2461  return -1;
2462  }
2463 
2464  /* sect_I_item */
2465  if (print_int_ary(sect->sect_I_item, sect->sect_I_index[sect->n_sect],
2466  COLS_INT_DEF, fp)) {
2467  return -1;
2468  }
2469 
2470  /* sect_R_index */
2471  if (print_int_ary(sect->sect_R_index, sect->n_sect + 1, COLS_INT_DEF, fp)) {
2472  return -1;
2473  }
2474 
2475  /* sect_R_item */
2476  if (print_double_ary(sect->sect_R_item, sect->sect_R_index[sect->n_sect],
2477  COLS_DOUBLE_DEF, fp)) {
2478  return -1;
2479  }
2480 
2481  return 0;
2482 }
2483 
2484 /*============================================================================*/
2485 /* */
2486 /* print material information */
2487 /* */
2488 /*============================================================================*/
2489 static int print_material_info(struct hecmwST_material *mat, FILE *fp) {
2490  /* n_mat */
2491  if (print_int(mat->n_mat, fp)) {
2492  return -1;
2493  }
2494 
2495  if (mat->n_mat == 0) return 0;
2496 
2497  /* n_mat_item */
2498  if (print_int(mat->n_mat_item, fp)) {
2499  return -1;
2500  }
2501 
2502  /* n_mat_subitem */
2503  if (print_int(mat->n_mat_subitem, fp)) {
2504  return -1;
2505  }
2506 
2507  /* n_mat_table */
2508  if (print_int(mat->n_mat_table, fp)) {
2509  return -1;
2510  }
2511 
2512  /* mat_name */
2513  if (print_string_ary(mat->mat_name, mat->n_mat, fp)) {
2514  return -1;
2515  }
2516 
2517  /* mat_item_index */
2518  if (print_int_ary(mat->mat_item_index, mat->n_mat + 1, COLS_INT_DEF, fp)) {
2519  return -1;
2520  }
2521 
2522  /* mat_subitem_index */
2523  if (print_int_ary(mat->mat_subitem_index, mat->n_mat_item + 1, COLS_INT_DEF,
2524  fp)) {
2525  return -1;
2526  }
2527 
2528  /* mat_table_index */
2529  if (print_int_ary(mat->mat_table_index, mat->n_mat_subitem + 1, COLS_INT_DEF,
2530  fp)) {
2531  return -1;
2532  }
2533 
2534  /* mat_val */
2535  if (print_double_ary(mat->mat_val, mat->n_mat_table, COLS_DOUBLE_DEF, fp)) {
2536  return -1;
2537  }
2538 
2539  /* mat_temp */
2540  if (print_double_ary(mat->mat_temp, mat->n_mat_table, COLS_DOUBLE_DEF, fp)) {
2541  return -1;
2542  }
2543 
2544  return 0;
2545 }
2546 
2547 /*============================================================================*/
2548 /* */
2549 /* print MPC group information */
2550 /* */
2551 /*============================================================================*/
2552 static int print_mpc_info(const struct hecmwST_mpc *mpc, FILE *fp) {
2553  /* n_mpc */
2554  if (print_int(mpc->n_mpc, fp)) {
2555  return -1;
2556  }
2557 
2558  if (mpc->n_mpc == 0) return 0;
2559 
2560  /* mpc_index */
2561  if (print_int_ary(mpc->mpc_index, mpc->n_mpc + 1, COLS_INT_DEF, fp)) {
2562  return -1;
2563  }
2564 
2565  /* mpc_item */
2566  if (print_int_ary(mpc->mpc_item, mpc->mpc_index[mpc->n_mpc], COLS_INT_DEF,
2567  fp)) {
2568  return -1;
2569  }
2570 
2571  /* mpc_dof */
2572  if (print_int_ary(mpc->mpc_dof, mpc->mpc_index[mpc->n_mpc], COLS_INT_DEF,
2573  fp)) {
2574  return -1;
2575  }
2576 
2577  /* mpc_val */
2578  if (print_double_ary(mpc->mpc_val, mpc->mpc_index[mpc->n_mpc],
2579  COLS_DOUBLE_DEF, fp)) {
2580  return -1;
2581  }
2582 
2583  /* mpc_const */
2584  if (print_double_ary(mpc->mpc_const, mpc->n_mpc, COLS_DOUBLE_DEF, fp)) {
2585  return -1;
2586  }
2587 
2588  return 0;
2589 }
2590 
2591 /*============================================================================*/
2592 /* */
2593 /* print amplitude information */
2594 /* */
2595 /*============================================================================*/
2596 static int print_amp_info(const struct hecmwST_amplitude *amp, FILE *fp) {
2597  /* n_amp */
2598  if (print_int(amp->n_amp, fp)) {
2599  return -1;
2600  }
2601 
2602  if (amp->n_amp == 0) return 0;
2603 
2604  /* amp_name */
2605  if (print_string_ary(amp->amp_name, amp->n_amp, fp)) {
2606  return -1;
2607  }
2608 
2609  /* amp_type_definition */
2610  if (print_int_ary(amp->amp_type_definition, amp->n_amp, COLS_INT_DEF, fp)) {
2611  return -1;
2612  }
2613 
2614  /* amp_type_time */
2615  if (print_int_ary(amp->amp_type_time, amp->n_amp, COLS_INT_DEF, fp)) {
2616  return -1;
2617  }
2618 
2619  /* amp_type_value */
2620  if (print_int_ary(amp->amp_type_value, amp->n_amp, COLS_INT_DEF, fp)) {
2621  return -1;
2622  }
2623 
2624  /* amp_index */
2625  if (print_int_ary(amp->amp_index, amp->n_amp + 1, COLS_INT_DEF, fp)) {
2626  return -1;
2627  }
2628 
2629  /* amp_val */
2630  if (print_double_ary(amp->amp_val, amp->amp_index[amp->n_amp],
2631  COLS_DOUBLE_DEF, fp)) {
2632  return -1;
2633  }
2634 
2635  /* amp_table */
2636  if (print_double_ary(amp->amp_table, amp->amp_index[amp->n_amp],
2637  COLS_DOUBLE_DEF, fp)) {
2638  return -1;
2639  }
2640 
2641  return 0;
2642 }
2643 
2644 /*============================================================================*/
2645 /* */
2646 /* print node group information */
2647 /* */
2648 /*============================================================================*/
2649 static int print_node_grp_info(const struct hecmwST_node_grp *grp, FILE *fp) {
2650  /* n_grp */
2651  if (print_int(grp->n_grp, fp)) {
2652  return -1;
2653  }
2654 
2655  if (grp->n_grp == 0) return 0;
2656 
2657  /* grp_name */
2658  if (print_string_ary(grp->grp_name, grp->n_grp, fp)) {
2659  return -1;
2660  }
2661 
2662  /* grp_index */
2663  if (print_int_ary(grp->grp_index, grp->n_grp + 1, COLS_INT_DEF, fp)) {
2664  return -1;
2665  }
2666 
2667  /* grp_item */
2668  if (print_int_ary(grp->grp_item, grp->grp_index[grp->n_grp], COLS_INT_DEF,
2669  fp)) {
2670  return -1;
2671  }
2672 
2673  return 0;
2674 }
2675 
2676 /*============================================================================*/
2677 /* */
2678 /* print element group information */
2679 /* */
2680 /*============================================================================*/
2681 static int print_elem_grp_info(const struct hecmwST_elem_grp *grp, FILE *fp) {
2682  /* n_grp */
2683  if (print_int(grp->n_grp, fp)) {
2684  return -1;
2685  }
2686 
2687  if (grp->n_grp == 0) return 0;
2688 
2689  /* grp_name */
2690  if (print_string_ary(grp->grp_name, grp->n_grp, fp)) {
2691  return -1;
2692  }
2693 
2694  /* grp_index */
2695  if (print_int_ary(grp->grp_index, grp->n_grp + 1, COLS_INT_DEF, fp)) {
2696  return -1;
2697  }
2698 
2699  /* grp_item */
2700  if (print_int_ary(grp->grp_item, grp->grp_index[grp->n_grp], COLS_INT_DEF,
2701  fp)) {
2702  return -1;
2703  }
2704 
2705  return 0;
2706 }
2707 
2708 /*============================================================================*/
2709 /* */
2710 /* print surface group information */
2711 /* */
2712 /*============================================================================*/
2713 static int print_surf_grp_info(const struct hecmwST_surf_grp *grp, FILE *fp) {
2714  /* n_grp */
2715  if (print_int(grp->n_grp, fp)) {
2716  return -1;
2717  }
2718 
2719  if (grp->n_grp == 0) return 0;
2720 
2721  /* grp_name */
2722  if (print_string_ary(grp->grp_name, grp->n_grp, fp)) {
2723  return -1;
2724  }
2725 
2726  /* grp_index */
2727  if (print_int_ary(grp->grp_index, grp->n_grp + 1, COLS_INT_DEF, fp)) {
2728  return -1;
2729  }
2730 
2731  /* grp_item */
2732  if (print_int_ary(grp->grp_item, grp->grp_index[grp->n_grp] * 2, COLS_TWO,
2733  fp)) {
2734  return -1;
2735  }
2736 
2737  return 0;
2738 }
2739 
2740 /*============================================================================*/
2741 /* */
2742 /* print refinement information */
2743 /* */
2744 /*============================================================================*/
2745 static int print_refine_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2746  /* number of refinement performed */
2747  if (print_int(mesh->n_refine, fp)) {
2748  return -1;
2749  }
2750 
2751  if (mesh->n_refine == 0 || mesh->n_subdomain == 1) return 0;
2752 
2753  if (mesh->n_node_gross > mesh->nn_internal) {
2754  /* node_old2new */
2755  if (print_int_ary(mesh->node_old2new, mesh->n_node_gross, COLS_INT_DEF,
2756  fp)) {
2757  return -1;
2758  }
2759 
2760  /* node_new2old */
2761  if (print_int_ary(mesh->node_new2old, mesh->n_node_gross, COLS_INT_DEF,
2762  fp)) {
2763  return -1;
2764  }
2765  }
2766 
2767  if (mesh->n_elem_gross > mesh->n_elem) {
2768  /* elem_old2new */
2769  if (print_int_ary(mesh->elem_old2new, mesh->n_elem_gross, COLS_INT_DEF,
2770  fp)) {
2771  return -1;
2772  }
2773 
2774  /* elem_new2old */
2775  if (print_int_ary(mesh->elem_new2old, mesh->n_elem_gross, COLS_INT_DEF,
2776  fp)) {
2777  return -1;
2778  }
2779  }
2780 
2781  return 0;
2782 }
2783 
2784 /*============================================================================*/
2785 /* */
2786 /* print contact information */
2787 /* */
2788 /*============================================================================*/
2789 static int print_contact_info(const struct hecmwST_contact_pair *cpair,
2790  FILE *fp) {
2791  /* n_pair */
2792  if (print_int(cpair->n_pair, fp)) {
2793  return -1;
2794  }
2795 
2796  if (cpair->n_pair == 0) return 0;
2797 
2798  /* name */
2799  if (print_string_ary(cpair->name, cpair->n_pair, fp)) {
2800  return -1;
2801  }
2802 
2803  /* type */
2804  if (print_int_ary(cpair->type, cpair->n_pair, COLS_INT_DEF, fp)) {
2805  return -1;
2806  }
2807 
2808  /* slave_grp_id */
2809  if (print_int_ary(cpair->slave_grp_id, cpair->n_pair, COLS_INT_DEF, fp)) {
2810  return -1;
2811  }
2812 
2813  /* slave_orisgrp_id */
2814  if (print_int_ary(cpair->slave_orisgrp_id, cpair->n_pair, COLS_INT_DEF, fp)) {
2815  return -1;
2816  }
2817 
2818  /* master_grp_id */
2819  if (print_int_ary(cpair->master_grp_id, cpair->n_pair, COLS_INT_DEF, fp)) {
2820  return -1;
2821  }
2822 
2823  return 0;
2824 }
2825 
2826 /*============================================================================*/
2827 /* */
2828 /* print HEC-MW distributed mesh format data */
2829 /* */
2830 /*============================================================================*/
2831 extern int HECMW_put_dist_mesh(const struct hecmwST_local_mesh *mesh,
2832  char *fname) {
2833  FILE *fp;
2834 
2835  if (mesh == NULL) return 0;
2836  if (fname == NULL) {
2837  HECMW_set_error(HECMW_IO_E5001, "Filename is NULL)");
2838  return -1;
2839  }
2840 
2841  if (HECMW_ctrl_is_subdir()) {
2842  if (HECMW_ctrl_make_subdir(fname)) return 0;
2843  }
2844 
2845  /* open file */
2846  if ((fp = fopen(fname, "w")) == NULL) {
2847  HECMW_set_error(HECMW_IO_E5001, "File: %s, %s", fname, strerror(errno));
2848  return -1;
2849  }
2850 
2851  /* header */
2852  if (print_header(mesh, fp)) {
2853  return -1;
2854  }
2855 
2856  /* global info. */
2857  if (print_global_info(mesh, fp)) {
2858  return -1;
2859  }
2860 
2861  /* node info. */
2862  if (print_node_info(mesh, fp)) {
2863  return -1;
2864  }
2865 
2866  /* element info. */
2867  if (print_elem_info(mesh, fp)) {
2868  return -1;
2869  }
2870 
2871  /* domain info & communication table */
2872  if (print_comm_info(mesh, fp)) {
2873  return -1;
2874  }
2875 
2876  /* adaptation info. */
2877  if (print_adapt_info(mesh, fp)) {
2878  return -1;
2879  }
2880 
2881  /* section info. */
2882  if (print_section_info(mesh->section, fp)) {
2883  return -1;
2884  }
2885 
2886  /* material info. */
2887  if (print_material_info(mesh->material, fp)) {
2888  return -1;
2889  }
2890 
2891  /* MPC group info. */
2892  if (print_mpc_info(mesh->mpc, fp)) {
2893  return -1;
2894  }
2895 
2896  /* amplitude info. */
2897  if (print_amp_info(mesh->amp, fp)) {
2898  return -1;
2899  }
2900 
2901  /* node group info. */
2902  if (print_node_grp_info(mesh->node_group, fp)) {
2903  return -1;
2904  }
2905 
2906  /* element group info. */
2907  if (print_elem_grp_info(mesh->elem_group, fp)) {
2908  return -1;
2909  }
2910 
2911  /* surface group info */
2912  if (print_surf_grp_info(mesh->surf_group, fp)) {
2913  return -1;
2914  }
2915 
2916  /* refinement info. */
2917  if (print_refine_info(mesh, fp)) {
2918  return -1;
2919  }
2920 
2921  /* contact info */
2922  if (print_contact_info(mesh->contact_pair, fp)) {
2923  return -1;
2924  }
2925 
2926  /* close file */
2927  if (fclose(fp)) {
2929  return -1;
2930  }
2931 
2932  return 0;
2933 }
HECMW_Comm HECMW_comm_get_comm(void)
Definition: hecmw_comm.c:751
int HECMW_comm_get_rank(void)
Definition: hecmw_comm.c:759
int HECMW_comm_get_size(void)
Definition: hecmw_comm.c:755
MPI_Comm HECMW_Comm
Definition: hecmw_config.h:30
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:70
int HECMW_ctrl_is_subdir(void)
int HECMW_ctrl_make_subdir(char *filename)
struct hecmwST_local_mesh * HECMW_dist_alloc()
struct hecmwST_local_mesh * mesh
Definition: hecmw_repart.h:71
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
#define LINEBUF_SIZE
struct hecmwST_local_mesh * HECMW_get_dist_mesh(char *fname)
#define HEADER_STRING
Definition: hecmw_io_dist.c:26
#define COLS_THREE
Definition: hecmw_io_dist.c:22
#define COLS_DOUBLE_DEF
Definition: hecmw_io_dist.c:20
#define COLS_TWO
Definition: hecmw_io_dist.c:21
#define COLS_INT_DEF
Definition: hecmw_io_dist.c:19
#define FILE_MAGIC_LEN
Definition: hecmw_io_dist.c:25
int HECMW_put_dist_mesh(const struct hecmwST_local_mesh *mesh, char *fname)
#define FILE_MAGIC
Definition: hecmw_io_dist.c:24
#define NULL
int HECMW_log(int loglv, const char *fmt,...)
Definition: hecmw_log.c:260
#define HECMW_LOG_DEBUG
Definition: hecmw_log.h:21
#define HECMW_calloc(nmemb, size)
Definition: hecmw_malloc.h:21
#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_IO_E5005
Definition: hecmw_msgno.h:162
#define HECMW_IO_E5001
Definition: hecmw_msgno.h:158
#define HECMW_IO_E5002
Definition: hecmw_msgno.h:159
#define HECMW_IO_E5003
Definition: hecmw_msgno.h:160
#define HECMW_IO_E5006
Definition: hecmw_msgno.h:163
#define HECMW_IO_E5004
Definition: hecmw_msgno.h:161
#define HECMW_FLAG_PARTTYPE_UNKNOWN
Definition: hecmw_struct.h:143
#define HECMW_FLAG_PARTCONTACT_UNKNOWN
Definition: hecmw_struct.h:149
#define HECMW_FLAG_PARTTYPE_NODEBASED
Definition: hecmw_struct.h:144
#define HECMW_FLAG_PARTTYPE_ELEMBASED
Definition: hecmw_struct.h:145
int * amp_type_definition
Definition: hecmw_struct.h:61
double * amp_table
Definition: hecmw_struct.h:72
struct hecmwST_section * section
Definition: hecmw_struct.h:245
struct hecmwST_amplitude * amp
Definition: hecmw_struct.h:248
struct hecmwST_material * material
Definition: hecmw_struct.h:246
struct hecmwST_mpc * mpc
Definition: hecmw_struct.h:247
struct hecmwST_node_grp * node_group
Definition: hecmw_struct.h:249
double * node_init_val_item
Definition: hecmw_struct.h:181
struct hecmwST_contact_pair * contact_pair
Definition: hecmw_struct.h:252
struct hecmwST_surf_grp * surf_group
Definition: hecmw_struct.h:251
long long * elem_node_index
Definition: hecmw_struct.h:195
char gridfile[HECMW_FILENAME_LEN+1]
Definition: hecmw_struct.h:154
char header[HECMW_HEADER_LEN+1]
Definition: hecmw_struct.h:157
HECMW_Comm HECMW_COMM
Definition: hecmw_struct.h:209
struct hecmwST_elem_grp * elem_group
Definition: hecmw_struct.h:250
int * when_i_was_refined_node
Definition: hecmw_struct.h:227
int * when_i_was_refined_elem
Definition: hecmw_struct.h:228
int * mat_subitem_index
Definition: hecmw_struct.h:42
double * mat_val
Definition: hecmw_struct.h:44
double * mat_temp
Definition: hecmw_struct.h:45
int * mpc_dof
Definition: hecmw_struct.h:52
double * mpc_val
Definition: hecmw_struct.h:53
double * mpc_const
Definition: hecmw_struct.h:54
int * mpc_index
Definition: hecmw_struct.h:50
int * mpc_item
Definition: hecmw_struct.h:51
double * sect_R_item
Definition: hecmw_struct.h:32
int * sect_mat_ID_index
Definition: hecmw_struct.h:27
int * sect_mat_ID_item
Definition: hecmw_struct.h:28