FrontISTR  5.8.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  rtc = sprintf(header + strlen(header), "%d", mesh->hecmw_flag_version);
1969  if (rtc < 0) {
1971  return -1;
1972  }
1973 
1974  if (print_string(header, fp)) {
1975  return -1;
1976  } else {
1977  return 0;
1978  }
1979 }
1980 
1981 /*----------------------------------------------------------------------------*/
1982 /* print global information */
1983 /*----------------------------------------------------------------------------*/
1984 static int print_global_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
1985  int flag_header;
1986 
1987  /* hecmw_flag_adapt */
1988  if (print_int(mesh->hecmw_flag_adapt, fp)) {
1989  return -1;
1990  }
1991 
1992  /* hecmw_flag_initcon */
1993  if (print_int(mesh->hecmw_flag_initcon, fp)) {
1994  return -1;
1995  }
1996 
1997  /* hecmw_flag_parttype */
1998  if (print_int(mesh->hecmw_flag_parttype, fp)) {
1999  return -1;
2000  }
2001 
2002  /* hecmw_flag_partdepth */
2003  if (print_int(mesh->hecmw_flag_partdepth, fp)) {
2004  return -1;
2005  }
2006 
2007  /* hecmw_flag_version */
2008  if (print_int(mesh->hecmw_flag_version, fp)) {
2009  return -1;
2010  }
2011 
2012  /* hecmw_flag_partcontact */
2013  if (print_int(mesh->hecmw_flag_partcontact, fp)) {
2014  return -1;
2015  }
2016 
2017  /* gridfile */
2018  if (print_string(mesh->gridfile, fp)) {
2019  return -1;
2020  }
2021 
2022  /* hecmw_n_files */
2023  if (print_int(mesh->hecmw_n_file, fp)) {
2024  return -1;
2025  }
2026 
2027  /* files */
2028  if (mesh->hecmw_n_file > 0) {
2029  if (print_string_ary(mesh->files, mesh->hecmw_n_file, fp)) {
2030  return -1;
2031  }
2032  }
2033 
2034  if (strlen(mesh->header) > 0) {
2035  /* flag for header */
2036  flag_header = 1;
2037  if (print_int(flag_header, fp)) {
2038  return -1;
2039  }
2040 
2041  /* header */
2042  if (print_string(mesh->header, fp)) {
2043  return -1;
2044  }
2045  } else {
2046  /* flag for header */
2047  flag_header = 0;
2048  if (print_int(flag_header, fp)) {
2049  return -1;
2050  }
2051  }
2052 
2053  /* zero_temp */
2054  if (print_double(mesh->zero_temp, fp)) {
2055  return -1;
2056  }
2057 
2058  return 0;
2059 }
2060 
2061 /*============================================================================*/
2062 /* */
2063 /* print node information */
2064 /* */
2065 /*============================================================================*/
2066 static int print_node_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2067  /* n_node */
2068  if (print_int(mesh->n_node, fp)) {
2069  return -1;
2070  }
2071 
2072  /* n_node_gross */
2073  if (print_int(mesh->n_node_gross, fp)) {
2074  return -1;
2075  }
2076 
2077  /* nn_middle */
2078  if (print_int(mesh->nn_middle, fp)) {
2079  return -1;
2080  }
2081 
2082  /* nn_internal */
2083  if (print_int(mesh->nn_internal, fp)) {
2084  return -1;
2085  }
2086 
2087  /* node_internal_list */
2090  if (print_int_ary(mesh->node_internal_list, mesh->nn_internal, COLS_INT_DEF,
2091  fp)) {
2092  return -1;
2093  }
2094  }
2095 
2096  /* node_ID */
2097  if (print_int_ary(mesh->node_ID, 2 * mesh->n_node_gross, COLS_TWO, fp)) {
2098  return -1;
2099  }
2100 
2101  /* global_node_ID */
2102  if (print_int_ary(mesh->global_node_ID, mesh->n_node_gross, COLS_INT_DEF,
2103  fp)) {
2104  return -1;
2105  }
2106 
2107  /* node */
2108  if (print_double_ary(mesh->node, 3 * mesh->n_node_gross, COLS_THREE, fp)) {
2109  return -1;
2110  }
2111 
2112  /* n_dof */
2113  if (print_int(mesh->n_dof, fp)) {
2114  return -1;
2115  }
2116 
2117  /* n_dof_grp */
2118  if (print_int(mesh->n_dof_grp, fp)) {
2119  return -1;
2120  }
2121 
2122  /* node_dof_index */
2123  if (print_int_ary(mesh->node_dof_index, mesh->n_dof_grp + 1, COLS_INT_DEF,
2124  fp)) {
2125  return -1;
2126  }
2127 
2128  /* node_dof_item */
2129  if (print_int_ary(mesh->node_dof_item, mesh->n_dof_grp, COLS_INT_DEF, fp)) {
2130  return -1;
2131  }
2132 
2133  if (mesh->hecmw_flag_initcon) {
2134  /* node_init_val_index */
2135  if (print_int_ary(mesh->node_init_val_index, mesh->n_node_gross + 1,
2136  COLS_INT_DEF, fp)) {
2137  return -1;
2138  }
2139 
2140  /* node_init_val_item */
2141  if (print_double_ary(mesh->node_init_val_item,
2143  COLS_DOUBLE_DEF, fp)) {
2144  return -1;
2145  }
2146  }
2147 
2148  /* node_val_index */
2149  /* node_val_item */
2150 
2151  return 0;
2152 }
2153 
2154 /*============================================================================*/
2155 /* */
2156 /* print element information */
2157 /* */
2158 /*============================================================================*/
2159 static int print_elem_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2160  /* n_elem */
2161  if (print_int(mesh->n_elem, fp)) {
2162  return -1;
2163  }
2164 
2165  /* n_elem_gross */
2166  if (print_int(mesh->n_elem_gross, fp)) {
2167  return -1;
2168  }
2169 
2170  /* ne_internal */
2171  if (print_int(mesh->ne_internal, fp)) {
2172  return -1;
2173  }
2174 
2175  /* elem_internal_list */
2178  if (print_int_ary(mesh->elem_internal_list, mesh->ne_internal, COLS_INT_DEF,
2179  fp)) {
2180  return -1;
2181  }
2182  }
2183 
2184  /* elem_ID */
2185  if (print_int_ary(mesh->elem_ID, 2 * mesh->n_elem_gross, COLS_TWO, fp)) {
2186  return -1;
2187  }
2188 
2189  /* global_elem_ID */
2190  if (print_int_ary(mesh->global_elem_ID, mesh->n_elem_gross, COLS_INT_DEF,
2191  fp)) {
2192  return -1;
2193  }
2194 
2195  /* elem_type */
2196  if (print_int_ary(mesh->elem_type, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2197  return -1;
2198  }
2199 
2200  /* n_elem_type */
2201  if (print_int(mesh->n_elem_type, fp)) {
2202  return -1;
2203  }
2204 
2205  /* elem_type_index */
2206  if (print_int_ary(mesh->elem_type_index, mesh->n_elem_type + 1, COLS_INT_DEF,
2207  fp)) {
2208  return -1;
2209  }
2210 
2211  /* elem_type_item */
2212  if (print_int_ary(mesh->elem_type_item, mesh->n_elem_type, COLS_INT_DEF,
2213  fp)) {
2214  return -1;
2215  }
2216 
2217  /* elem_node_index */
2218  if (print_longlong_as_int_ary(mesh->elem_node_index, mesh->n_elem_gross + 1,
2219  COLS_INT_DEF, fp, "elem_node_index")) {
2220  return -1;
2221  }
2222 
2223  /* elem_node_item */
2224  if (print_int_ary(mesh->elem_node_item,
2226  fp)) {
2227  return -1;
2228  }
2229 
2230  /* section_ID */
2231  if (print_int_ary(mesh->section_ID, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2232  return -1;
2233  }
2234 
2235  /* elem_mat_ID_index */
2236  if (print_int_ary(mesh->elem_mat_ID_index, mesh->n_elem_gross + 1,
2237  COLS_INT_DEF, fp)) {
2238  return -1;
2239  }
2240 
2241  /* elem_mat_ID_item */
2242  if (print_int_ary(mesh->elem_mat_ID_item,
2244  fp)) {
2245  return -1;
2246  }
2247 
2248  /* n_elem_mat_ID */
2249  if (print_int(mesh->n_elem_mat_ID, fp)) {
2250  return -1;
2251  }
2252 
2253  /* elem_mat_int_index */
2254  /* elem_mat_int_item */
2255 
2256  /* elem_val_index */
2257  /* elem_val_item */
2258 
2259  return 0;
2260 }
2261 
2262 /*============================================================================*/
2263 /* */
2264 /* print domain information & communication tables */
2265 /* */
2266 /*============================================================================*/
2267 static int print_comm_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2268  /* zero */
2269  if (print_int(mesh->zero, fp)) {
2270  return -1;
2271  }
2272 
2273  /* HECMW_COMM */
2274  if (print_comm(mesh->HECMW_COMM, fp)) {
2275  return -1;
2276  }
2277 
2278  /* PETOT */
2279  if (print_int(mesh->PETOT, fp)) {
2280  return -1;
2281  }
2282 
2283  /* PEsmpTOT */
2284  if (print_int(mesh->PEsmpTOT, fp)) {
2285  return -1;
2286  }
2287 
2288  /* my_rank */
2289  if (print_int(mesh->my_rank, fp)) {
2290  return -1;
2291  }
2292 
2293  /* errnof */
2294  if (print_int(mesh->errnof, fp)) {
2295  return -1;
2296  }
2297 
2298  /* n_subdomain */
2299  if (print_int(mesh->n_subdomain, fp)) {
2300  return -1;
2301  }
2302 
2303  /* n_neighbor_pd */
2304  if (print_int(mesh->n_neighbor_pe, fp)) {
2305  return -1;
2306  }
2307 
2308  if (mesh->n_neighbor_pe == 0) return 0;
2309 
2310  /* neighbor_pe */
2311  if (print_int_ary(mesh->neighbor_pe, mesh->n_neighbor_pe, COLS_INT_DEF, fp)) {
2312  return -1;
2313  }
2314 
2315  /* import_index */
2316  if (print_int_ary(mesh->import_index, mesh->n_neighbor_pe + 1, COLS_INT_DEF,
2317  fp)) {
2318  return -1;
2319  }
2320 
2321  /* import_item */
2322  if (print_int_ary(mesh->import_item, mesh->import_index[mesh->n_neighbor_pe],
2323  COLS_INT_DEF, fp)) {
2324  return -1;
2325  }
2326 
2327  /* export_index */
2328  if (print_int_ary(mesh->export_index, mesh->n_neighbor_pe + 1, COLS_INT_DEF,
2329  fp)) {
2330  return -1;
2331  }
2332 
2333  /* export_item */
2334  if (print_int_ary(mesh->export_item, mesh->export_index[mesh->n_neighbor_pe],
2335  COLS_INT_DEF, fp)) {
2336  return -1;
2337  }
2338 
2339  /* shared_index */
2340  if (print_int_ary(mesh->shared_index, mesh->n_neighbor_pe + 1, COLS_INT_DEF,
2341  fp)) {
2342  return -1;
2343  }
2344 
2345  /* shared_item */
2346  if (print_int_ary(mesh->shared_item, mesh->shared_index[mesh->n_neighbor_pe],
2347  COLS_INT_DEF, fp)) {
2348  return -1;
2349  }
2350 
2351  return 0;
2352 }
2353 
2354 /*============================================================================*/
2355 /* */
2356 /* print adaptation information */
2357 /* */
2358 /*============================================================================*/
2359 static int print_adapt_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2360  if (mesh->hecmw_flag_adapt == 0) return 0;
2361 
2362  /* coarse_grid_level */
2363  if (print_int(mesh->coarse_grid_level, fp)) {
2364  return -1;
2365  }
2366 
2367  /* n_adapt */
2368  if (print_int(mesh->n_adapt, fp)) {
2369  return -1;
2370  }
2371 
2372  /* when_i_was_refined_node */
2373  if (print_int_ary(mesh->when_i_was_refined_node, mesh->n_node_gross,
2374  COLS_INT_DEF, fp)) {
2375  return -1;
2376  }
2377 
2378  /* when_i_was_refined_elem */
2379  if (print_int_ary(mesh->when_i_was_refined_elem, mesh->n_elem_gross,
2380  COLS_INT_DEF, fp)) {
2381  return -1;
2382  }
2383 
2384  /* adapt_parent_type */
2385  if (print_int_ary(mesh->adapt_parent_type, mesh->n_elem_gross, COLS_INT_DEF,
2386  fp)) {
2387  return -1;
2388  }
2389 
2390  /* adapt_type */
2391  if (print_int_ary(mesh->adapt_type, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2392  return -1;
2393  }
2394 
2395  /* adapt_level */
2396  if (print_int_ary(mesh->adapt_level, mesh->n_elem_gross, COLS_INT_DEF, fp)) {
2397  return -1;
2398  }
2399 
2400  /* adapt_parent */
2401  if (print_int_ary(mesh->adapt_parent, 2 * mesh->n_elem_gross, COLS_TWO, fp)) {
2402  return -1;
2403  }
2404 
2405  /* adapt_children_index */
2406  if (print_int_ary(mesh->adapt_children_index, mesh->n_elem_gross + 1,
2407  COLS_INT_DEF, fp)) {
2408  return -1;
2409  }
2410 
2411  /* adapt_children_item */
2412  if (print_int_ary(mesh->adapt_children_item,
2414  COLS_TWO, fp)) {
2415  return -1;
2416  }
2417 
2418  return 0;
2419 }
2420 
2421 /*============================================================================*/
2422 /* */
2423 /* print section information */
2424 /* */
2425 /*============================================================================*/
2426 static int print_section_info(const struct hecmwST_section *sect, FILE *fp) {
2427  /* n_sect */
2428  if (print_int(sect->n_sect, fp)) {
2429  return -1;
2430  }
2431 
2432  if (sect->n_sect == 0) return 0;
2433 
2434  /* sect_type */
2435  if (print_int_ary(sect->sect_type, sect->n_sect, COLS_INT_DEF, fp)) {
2436  return -1;
2437  }
2438 
2439  /* sect_opt */
2440  if (print_int_ary(sect->sect_opt, sect->n_sect, COLS_INT_DEF, fp)) {
2441  return -1;
2442  }
2443 
2444  /* sect_mat_ID_index */
2445  if (print_int_ary(sect->sect_mat_ID_index, sect->n_sect + 1, COLS_INT_DEF,
2446  fp)) {
2447  return -1;
2448  }
2449 
2450  /* sect_mat_ID_item */
2451  if (print_int_ary(sect->sect_mat_ID_item,
2452  sect->sect_mat_ID_index[sect->n_sect], COLS_INT_DEF, fp)) {
2453  return -1;
2454  }
2455 
2456  /* sect_I_index */
2457  if (print_int_ary(sect->sect_I_index, sect->n_sect + 1, COLS_INT_DEF, fp)) {
2458  return -1;
2459  }
2460 
2461  /* sect_I_item */
2462  if (print_int_ary(sect->sect_I_item, sect->sect_I_index[sect->n_sect],
2463  COLS_INT_DEF, fp)) {
2464  return -1;
2465  }
2466 
2467  /* sect_R_index */
2468  if (print_int_ary(sect->sect_R_index, sect->n_sect + 1, COLS_INT_DEF, fp)) {
2469  return -1;
2470  }
2471 
2472  /* sect_R_item */
2473  if (print_double_ary(sect->sect_R_item, sect->sect_R_index[sect->n_sect],
2474  COLS_DOUBLE_DEF, fp)) {
2475  return -1;
2476  }
2477 
2478  return 0;
2479 }
2480 
2481 /*============================================================================*/
2482 /* */
2483 /* print material information */
2484 /* */
2485 /*============================================================================*/
2486 static int print_material_info(struct hecmwST_material *mat, FILE *fp) {
2487  /* n_mat */
2488  if (print_int(mat->n_mat, fp)) {
2489  return -1;
2490  }
2491 
2492  if (mat->n_mat == 0) return 0;
2493 
2494  /* n_mat_item */
2495  if (print_int(mat->n_mat_item, fp)) {
2496  return -1;
2497  }
2498 
2499  /* n_mat_subitem */
2500  if (print_int(mat->n_mat_subitem, fp)) {
2501  return -1;
2502  }
2503 
2504  /* n_mat_table */
2505  if (print_int(mat->n_mat_table, fp)) {
2506  return -1;
2507  }
2508 
2509  /* mat_name */
2510  if (print_string_ary(mat->mat_name, mat->n_mat, fp)) {
2511  return -1;
2512  }
2513 
2514  /* mat_item_index */
2515  if (print_int_ary(mat->mat_item_index, mat->n_mat + 1, COLS_INT_DEF, fp)) {
2516  return -1;
2517  }
2518 
2519  /* mat_subitem_index */
2520  if (print_int_ary(mat->mat_subitem_index, mat->n_mat_item + 1, COLS_INT_DEF,
2521  fp)) {
2522  return -1;
2523  }
2524 
2525  /* mat_table_index */
2526  if (print_int_ary(mat->mat_table_index, mat->n_mat_subitem + 1, COLS_INT_DEF,
2527  fp)) {
2528  return -1;
2529  }
2530 
2531  /* mat_val */
2532  if (print_double_ary(mat->mat_val, mat->n_mat_table, COLS_DOUBLE_DEF, fp)) {
2533  return -1;
2534  }
2535 
2536  /* mat_temp */
2537  if (print_double_ary(mat->mat_temp, mat->n_mat_table, COLS_DOUBLE_DEF, fp)) {
2538  return -1;
2539  }
2540 
2541  return 0;
2542 }
2543 
2544 /*============================================================================*/
2545 /* */
2546 /* print MPC group information */
2547 /* */
2548 /*============================================================================*/
2549 static int print_mpc_info(const struct hecmwST_mpc *mpc, FILE *fp) {
2550  /* n_mpc */
2551  if (print_int(mpc->n_mpc, fp)) {
2552  return -1;
2553  }
2554 
2555  if (mpc->n_mpc == 0) return 0;
2556 
2557  /* mpc_index */
2558  if (print_int_ary(mpc->mpc_index, mpc->n_mpc + 1, COLS_INT_DEF, fp)) {
2559  return -1;
2560  }
2561 
2562  /* mpc_item */
2563  if (print_int_ary(mpc->mpc_item, mpc->mpc_index[mpc->n_mpc], COLS_INT_DEF,
2564  fp)) {
2565  return -1;
2566  }
2567 
2568  /* mpc_dof */
2569  if (print_int_ary(mpc->mpc_dof, mpc->mpc_index[mpc->n_mpc], COLS_INT_DEF,
2570  fp)) {
2571  return -1;
2572  }
2573 
2574  /* mpc_val */
2575  if (print_double_ary(mpc->mpc_val, mpc->mpc_index[mpc->n_mpc],
2576  COLS_DOUBLE_DEF, fp)) {
2577  return -1;
2578  }
2579 
2580  /* mpc_const */
2581  if (print_double_ary(mpc->mpc_const, mpc->n_mpc, COLS_DOUBLE_DEF, fp)) {
2582  return -1;
2583  }
2584 
2585  return 0;
2586 }
2587 
2588 /*============================================================================*/
2589 /* */
2590 /* print amplitude information */
2591 /* */
2592 /*============================================================================*/
2593 static int print_amp_info(const struct hecmwST_amplitude *amp, FILE *fp) {
2594  /* n_amp */
2595  if (print_int(amp->n_amp, fp)) {
2596  return -1;
2597  }
2598 
2599  if (amp->n_amp == 0) return 0;
2600 
2601  /* amp_name */
2602  if (print_string_ary(amp->amp_name, amp->n_amp, fp)) {
2603  return -1;
2604  }
2605 
2606  /* amp_type_definition */
2607  if (print_int_ary(amp->amp_type_definition, amp->n_amp, COLS_INT_DEF, fp)) {
2608  return -1;
2609  }
2610 
2611  /* amp_type_time */
2612  if (print_int_ary(amp->amp_type_time, amp->n_amp, COLS_INT_DEF, fp)) {
2613  return -1;
2614  }
2615 
2616  /* amp_type_value */
2617  if (print_int_ary(amp->amp_type_value, amp->n_amp, COLS_INT_DEF, fp)) {
2618  return -1;
2619  }
2620 
2621  /* amp_index */
2622  if (print_int_ary(amp->amp_index, amp->n_amp + 1, COLS_INT_DEF, fp)) {
2623  return -1;
2624  }
2625 
2626  /* amp_val */
2627  if (print_double_ary(amp->amp_val, amp->amp_index[amp->n_amp],
2628  COLS_DOUBLE_DEF, fp)) {
2629  return -1;
2630  }
2631 
2632  /* amp_table */
2633  if (print_double_ary(amp->amp_table, amp->amp_index[amp->n_amp],
2634  COLS_DOUBLE_DEF, fp)) {
2635  return -1;
2636  }
2637 
2638  return 0;
2639 }
2640 
2641 /*============================================================================*/
2642 /* */
2643 /* print node group information */
2644 /* */
2645 /*============================================================================*/
2646 static int print_node_grp_info(const struct hecmwST_node_grp *grp, FILE *fp) {
2647  /* n_grp */
2648  if (print_int(grp->n_grp, fp)) {
2649  return -1;
2650  }
2651 
2652  if (grp->n_grp == 0) return 0;
2653 
2654  /* grp_name */
2655  if (print_string_ary(grp->grp_name, grp->n_grp, fp)) {
2656  return -1;
2657  }
2658 
2659  /* grp_index */
2660  if (print_int_ary(grp->grp_index, grp->n_grp + 1, COLS_INT_DEF, fp)) {
2661  return -1;
2662  }
2663 
2664  /* grp_item */
2665  if (print_int_ary(grp->grp_item, grp->grp_index[grp->n_grp], COLS_INT_DEF,
2666  fp)) {
2667  return -1;
2668  }
2669 
2670  return 0;
2671 }
2672 
2673 /*============================================================================*/
2674 /* */
2675 /* print element group information */
2676 /* */
2677 /*============================================================================*/
2678 static int print_elem_grp_info(const struct hecmwST_elem_grp *grp, FILE *fp) {
2679  /* n_grp */
2680  if (print_int(grp->n_grp, fp)) {
2681  return -1;
2682  }
2683 
2684  if (grp->n_grp == 0) return 0;
2685 
2686  /* grp_name */
2687  if (print_string_ary(grp->grp_name, grp->n_grp, fp)) {
2688  return -1;
2689  }
2690 
2691  /* grp_index */
2692  if (print_int_ary(grp->grp_index, grp->n_grp + 1, COLS_INT_DEF, fp)) {
2693  return -1;
2694  }
2695 
2696  /* grp_item */
2697  if (print_int_ary(grp->grp_item, grp->grp_index[grp->n_grp], COLS_INT_DEF,
2698  fp)) {
2699  return -1;
2700  }
2701 
2702  return 0;
2703 }
2704 
2705 /*============================================================================*/
2706 /* */
2707 /* print surface group information */
2708 /* */
2709 /*============================================================================*/
2710 static int print_surf_grp_info(const struct hecmwST_surf_grp *grp, FILE *fp) {
2711  /* n_grp */
2712  if (print_int(grp->n_grp, fp)) {
2713  return -1;
2714  }
2715 
2716  if (grp->n_grp == 0) return 0;
2717 
2718  /* grp_name */
2719  if (print_string_ary(grp->grp_name, grp->n_grp, fp)) {
2720  return -1;
2721  }
2722 
2723  /* grp_index */
2724  if (print_int_ary(grp->grp_index, grp->n_grp + 1, COLS_INT_DEF, fp)) {
2725  return -1;
2726  }
2727 
2728  /* grp_item */
2729  if (print_int_ary(grp->grp_item, grp->grp_index[grp->n_grp] * 2, COLS_TWO,
2730  fp)) {
2731  return -1;
2732  }
2733 
2734  return 0;
2735 }
2736 
2737 /*============================================================================*/
2738 /* */
2739 /* print refinement information */
2740 /* */
2741 /*============================================================================*/
2742 static int print_refine_info(const struct hecmwST_local_mesh *mesh, FILE *fp) {
2743  /* number of refinement performed */
2744  if (print_int(mesh->n_refine, fp)) {
2745  return -1;
2746  }
2747 
2748  if (mesh->n_refine == 0 || mesh->n_subdomain == 1) return 0;
2749 
2750  if (mesh->n_node_gross > mesh->nn_internal) {
2751  /* node_old2new */
2752  if (print_int_ary(mesh->node_old2new, mesh->n_node_gross, COLS_INT_DEF,
2753  fp)) {
2754  return -1;
2755  }
2756 
2757  /* node_new2old */
2758  if (print_int_ary(mesh->node_new2old, mesh->n_node_gross, COLS_INT_DEF,
2759  fp)) {
2760  return -1;
2761  }
2762  }
2763 
2764  if (mesh->n_elem_gross > mesh->n_elem) {
2765  /* elem_old2new */
2766  if (print_int_ary(mesh->elem_old2new, mesh->n_elem_gross, COLS_INT_DEF,
2767  fp)) {
2768  return -1;
2769  }
2770 
2771  /* elem_new2old */
2772  if (print_int_ary(mesh->elem_new2old, mesh->n_elem_gross, COLS_INT_DEF,
2773  fp)) {
2774  return -1;
2775  }
2776  }
2777 
2778  return 0;
2779 }
2780 
2781 /*============================================================================*/
2782 /* */
2783 /* print contact information */
2784 /* */
2785 /*============================================================================*/
2786 static int print_contact_info(const struct hecmwST_contact_pair *cpair,
2787  FILE *fp) {
2788  /* n_pair */
2789  if (print_int(cpair->n_pair, fp)) {
2790  return -1;
2791  }
2792 
2793  if (cpair->n_pair == 0) return 0;
2794 
2795  /* name */
2796  if (print_string_ary(cpair->name, cpair->n_pair, fp)) {
2797  return -1;
2798  }
2799 
2800  /* type */
2801  if (print_int_ary(cpair->type, cpair->n_pair, COLS_INT_DEF, fp)) {
2802  return -1;
2803  }
2804 
2805  /* slave_grp_id */
2806  if (print_int_ary(cpair->slave_grp_id, cpair->n_pair, COLS_INT_DEF, fp)) {
2807  return -1;
2808  }
2809 
2810  /* slave_orisgrp_id */
2811  if (print_int_ary(cpair->slave_orisgrp_id, cpair->n_pair, COLS_INT_DEF, fp)) {
2812  return -1;
2813  }
2814 
2815  /* master_grp_id */
2816  if (print_int_ary(cpair->master_grp_id, cpair->n_pair, COLS_INT_DEF, fp)) {
2817  return -1;
2818  }
2819 
2820  return 0;
2821 }
2822 
2823 /*============================================================================*/
2824 /* */
2825 /* print HEC-MW distributed mesh format data */
2826 /* */
2827 /*============================================================================*/
2828 extern int HECMW_put_dist_mesh(const struct hecmwST_local_mesh *mesh,
2829  char *fname) {
2830  FILE *fp;
2831 
2832  if (mesh == NULL) return 0;
2833  if (fname == NULL) {
2834  HECMW_set_error(HECMW_IO_E5001, "Filename is NULL)");
2835  return -1;
2836  }
2837 
2838  if (HECMW_ctrl_is_subdir()) {
2839  if (HECMW_ctrl_make_subdir(fname)) return 0;
2840  }
2841 
2842  /* open file */
2843  if ((fp = fopen(fname, "w")) == NULL) {
2844  HECMW_set_error(HECMW_IO_E5001, "File: %s, %s", fname, strerror(errno));
2845  return -1;
2846  }
2847 
2848  /* header */
2849  if (print_header(mesh, fp)) {
2850  return -1;
2851  }
2852 
2853  /* global info. */
2854  if (print_global_info(mesh, fp)) {
2855  return -1;
2856  }
2857 
2858  /* node info. */
2859  if (print_node_info(mesh, fp)) {
2860  return -1;
2861  }
2862 
2863  /* element info. */
2864  if (print_elem_info(mesh, fp)) {
2865  return -1;
2866  }
2867 
2868  /* domain info & communication table */
2869  if (print_comm_info(mesh, fp)) {
2870  return -1;
2871  }
2872 
2873  /* adaptation info. */
2874  if (print_adapt_info(mesh, fp)) {
2875  return -1;
2876  }
2877 
2878  /* section info. */
2879  if (print_section_info(mesh->section, fp)) {
2880  return -1;
2881  }
2882 
2883  /* material info. */
2884  if (print_material_info(mesh->material, fp)) {
2885  return -1;
2886  }
2887 
2888  /* MPC group info. */
2889  if (print_mpc_info(mesh->mpc, fp)) {
2890  return -1;
2891  }
2892 
2893  /* amplitude info. */
2894  if (print_amp_info(mesh->amp, fp)) {
2895  return -1;
2896  }
2897 
2898  /* node group info. */
2899  if (print_node_grp_info(mesh->node_group, fp)) {
2900  return -1;
2901  }
2902 
2903  /* element group info. */
2904  if (print_elem_grp_info(mesh->elem_group, fp)) {
2905  return -1;
2906  }
2907 
2908  /* surface group info */
2909  if (print_surf_grp_info(mesh->surf_group, fp)) {
2910  return -1;
2911  }
2912 
2913  /* refinement info. */
2914  if (print_refine_info(mesh, fp)) {
2915  return -1;
2916  }
2917 
2918  /* contact info */
2919  if (print_contact_info(mesh->contact_pair, fp)) {
2920  return -1;
2921  }
2922 
2923  /* close file */
2924  if (fclose(fp)) {
2926  return -1;
2927  }
2928 
2929  return 0;
2930 }
HECMW_Comm HECMW_comm_get_comm(void)
Definition: hecmw_comm.c:699
int HECMW_comm_get_rank(void)
Definition: hecmw_comm.c:707
int HECMW_comm_get_size(void)
Definition: hecmw_comm.c:703
MPI_Comm HECMW_Comm
Definition: hecmw_config.h:30
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:68
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