FrontISTR  5.7.0
Large-scale structural analysis program with finit element method
fstr_rmerge.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 /*
7  * Merge FSTR result output files into a single file.
8  * Control file and mesh file used in calculations are required.
9  */
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 
15 #include "hecmw_util.h"
16 #include "hecmw_result_io_bin.h"
17 #include "hecmw_result_io_txt.h"
18 
19 #include "fstr_rmerge_util.h"
20 
21 #define BINARY_DEFAULT 0
22 #define NRANK_DEFAULT 0
23 #define STRID_DEFAULT 0
24 #define ENDID_DEFAULT -1
25 #define INTID_DEFAULT 1
26 
27 void error_stop(void) {
28  char* msg;
29  HECMW_get_error(&msg);
30  fputs(msg, stderr);
31  exit(-1);
32 }
33 
34 void help(void) {
35  printf(" HECMW Result File Merger\n");
36  printf("usage) rmerge [options] out_fileheader\n");
37  printf("[option]\n");
38  printf(" -h : help\n");
39  printf(" -o [type] : output file type (binary/text, default:%s)\n",
40  (BINARY_DEFAULT == 1) ? "binary" : "text");
41  printf(" -n [rank] : number of ranks (default:%d)\n", NRANK_DEFAULT);
42  printf(" -s [step] : start step number (default:%d)\n", STRID_DEFAULT);
43  printf(" -e [step] : end step number (default:%d)\n", ENDID_DEFAULT);
44  printf(" -i [step] : interval step number (default:%d)\n", INTID_DEFAULT);
45 }
46 
47 void set_fname(int argc, char** argv, char* out_fheader, int* binary,
48  int *nrank, int *strid, int *endid, int *intid) {
49  int i;
50  char* fheader = NULL;
51 
52  for (i = 1; i < argc; i++) {
53  if (!strcmp(argv[i], "-h")) {
54  help();
55  exit(-1);
56  } else if (!strcmp(argv[i], "-o")) {
57  if (argc == i + 1) {
58  fprintf(stderr, "Error : parameter required after %s\n", argv[i]);
59  exit(-1);
60  }
61  i++;
62  if (!strcmp(argv[i], "binary")) {
63  *binary = 1;
64  } else if (!strcmp(argv[i], "text")) {
65  *binary = 0;
66  } else {
67  fprintf(stderr, "Error : text or binary is required after -o\n");
68  exit(-1);
69  }
70  } else if (strcmp(argv[i], "-n") == 0) {
71  if (argc == i + 1) {
72  fprintf(stderr, "Error : parameter required after %s\n", argv[i]);
73  exit(-1);
74  }
75  i++;
76  if (sscanf(argv[i], "%d", nrank) != 1) {
77  fprintf(stderr,
78  "Error : parameter %s cannot be converted to number of ranks\n",
79  argv[i]);
80  exit(-1);
81  }
82  } else if (strcmp(argv[i], "-s") == 0) {
83  if (argc == i + 1) {
84  fprintf(stderr, "Error : parameter required after %s\n", argv[i]);
85  exit(-1);
86  }
87  i++;
88  if (sscanf(argv[i], "%d", strid) != 1) {
89  fprintf(
90  stderr,
91  "Error : parameter %s cannot be converted to start step number\n",
92  argv[i]);
93  exit(-1);
94  }
95  } else if (strcmp(argv[i], "-e") == 0) {
96  if (argc == i + 1) {
97  fprintf(stderr, "Error : parameter required after %s\n", argv[i]);
98  exit(-1);
99  }
100  i++;
101  if (sscanf(argv[i], "%d", endid) != 1) {
102  fprintf(stderr,
103  "Error : parameter %s cannot be converted to end step number\n",
104  argv[i]);
105  exit(-1);
106  }
107  } else if (strcmp(argv[i], "-i") == 0) {
108  if (argc == i + 1) {
109  fprintf(stderr, "Error : parameter required after %s\n", argv[i]);
110  exit(-1);
111  }
112  i++;
113  if (sscanf(argv[i], "%d", intid) != 1) {
114  fprintf(stderr,
115  "Error : parameter %s cannot be converted to interval step "
116  "number\n",
117  argv[i]);
118  exit(-1);
119  }
120  } else {
121  fheader = argv[i];
122  }
123  }
124 
125  if (!fheader) {
126  help();
127  exit(-1);
128  } else {
129  strcpy(out_fheader, fheader);
130  }
131 }
132 
133 int main(int argc, char** argv) {
134  int binary = BINARY_DEFAULT;
135  int nrank = NRANK_DEFAULT;
136  int strid = STRID_DEFAULT;
137  int endid = ENDID_DEFAULT;
138  int intid = INTID_DEFAULT;
139  int area_n, step_n, refine, fg_text;
140  int step, rcode;
141  char out_fheader[HECMW_HEADER_LEN + 1];
142  char out_fname[HECMW_FILENAME_LEN + 1];
143  struct hecmwST_local_mesh** mesh;
144  struct hecmwST_local_mesh* glmesh;
145  struct hecmwST_result_data* data;
146  fstr_res_info** res;
147  fstr_glt* glt;
148  char header[HECMW_HEADER_LEN + 1];
149  char comment[HECMW_MSG_LEN + 1];
150  char* fileheader;
151  char dirname[HECMW_HEADER_LEN + 1];
152  char buff[HECMW_HEADER_LEN + 1];
153  char *ptoken, *ntoken;
154 
155  fstr_set_log_fp(stderr);
156 
157  if (HECMW_init(&argc, &argv)) error_stop();
158 
159  set_fname(argc, argv, out_fheader, &binary, &nrank, &strid, &endid, &intid);
160  fstr_out_log("out file name header is %s\n", out_fheader);
161 
162  mesh = fstr_get_all_local_mesh("fstrMSH", nrank, &area_n, &refine);
163  if (!mesh) error_stop();
164 
165  fstr_out_log("table creating .. \n");
166  glt = fstr_create_glt(mesh, area_n);
167  if (!glt) {
168  fprintf(stderr, "ERROR : Cannot create global_local table.\n");
169  fstr_free_mesh(mesh, area_n);
170  exit(-1);
171  }
172 
173  glmesh = fstr_create_glmesh(glt);
174  if (!glmesh) {
175  fprintf(stderr, "ERROR : Cannot create global table.\n");
176  fstr_free_mesh(mesh, area_n);
177  fstr_free_glt(glt);
178  exit(-1);
179  }
180 
181  fstr_free_mesh(mesh, area_n);
182 
183  step_n = (endid > -1) ? endid : fstr_get_step_n("fstrRES", nrank);
184 
185  for (step = strid; step <= step_n; step++) {
186  if ((step % intid) != 0 && step != step_n) continue;
187 
188  fstr_out_log("step:%d .. reading .. ", step);
189  res = fstr_get_all_result("fstrRES", step, area_n, refine, nrank);
190  if (!res) {
191  fstr_free_result(res, area_n);
192  continue;
193  }
194  fstr_out_log("end\n");
195 
196  fstr_out_log("step:%d .. combining .. ", step);
197  data = fstr_all_result(glt, res, refine);
198  if (!data) {
199  fprintf(stderr, "ERROR : Cannot combine result structure.\n");
200  fstr_free_glt(glt);
201  fstr_free_glmesh(glmesh);
202  fstr_free_result(res, area_n);
203  exit(-1);
204  }
205  fstr_out_log("end\n");
206 
207  if (nrank == 0) {
208  if ((fileheader = HECMW_ctrl_get_result_fileheader("fstrRES", step,
209  &fg_text)) == NULL)
210  return 0;
211  } else {
212  if ((fileheader = HECMW_ctrl_get_result_fileheader_sub(
213  "fstrRES", step, nrank, 0, &fg_text)) == NULL)
214  return 0;
215  }
216  strcpy(buff, fileheader);
217  strcpy(dirname, "");
218  ptoken = strtok(buff, "/");
219  ntoken = strtok(NULL, "/");
220  while (ntoken) {
221  strcat(dirname, ptoken);
222  strcat(dirname, "/");
223  ptoken = ntoken;
224  ntoken = strtok(NULL, "/");
225  }
226  sprintf(out_fname, "%s%s.%d", dirname, out_fheader, step);
227  fstr_out_log("output to %s .. ", out_fname);
228  HECMW_result_get_header(header);
229  HECMW_result_get_comment(comment);
230  HECMW_result_init(glmesh, step, header, comment);
231  if (binary) {
232  rcode = HECMW_result_io_bin_write_ST_by_fname(out_fname, data, glt->node_n,
233  glt->elem_n, header, comment);
234  } else {
235  rcode = HECMW_result_io_txt_write_ST_by_fname(out_fname, data, glt->node_n,
236  glt->elem_n, header, comment);
237  }
238  if (rcode) {
239  fprintf(stderr, "ERROR : Cannot open/write file %s\n", out_fname);
240  fstr_free_glt(glt);
241  fstr_free_glmesh(glmesh);
242  fstr_free_result(res, area_n);
244  exit(-1);
245  }
246  fstr_out_log("end\n");
247 
248  fstr_free_result(res, area_n);
250  }
251 
252  fstr_free_glt(glt);
253  fstr_free_glmesh(glmesh);
254 
255  HECMW_finalize();
256 
257  exit(0);
258 }
fstr_get_step_n
int fstr_get_step_n(char *name_ID, int nrank)
Check the number of steps (check for the existence of files)
Definition: fstr_rmerge_util.c:161
hecmwST_result_data
Definition: hecmw_result.h:11
HECMW_result_get_comment
char * HECMW_result_get_comment(char *buff)
Definition: hecmw_result.c:209
HECMW_ctrl_get_result_fileheader_sub
char * HECMW_ctrl_get_result_fileheader_sub(char *name_ID, int istep, int n_rank, int i_rank, int *fg_text)
Definition: hecmw_control.c:2331
hecmw_result_io_txt.h
HECMW_result_get_header
char * HECMW_result_get_header(char *buff)
Definition: hecmw_result.c:204
fstr_get_all_local_mesh
struct hecmwST_local_mesh ** fstr_get_all_local_mesh(char *name_ID, int nrank, int *area_number, int *refine)
Read all distributed meshes.
Definition: fstr_rmerge_util.c:95
endid
int endid
Definition: hecmw_res_type_conv.c:16
mesh
struct hecmwST_local_mesh * mesh
Definition: hecmw_repart.h:71
error_stop
void error_stop(void)
Definition: fstr_rmerge.c:27
hecmwST_local_mesh
Definition: hecmw_struct.h:139
fstr_glt::elem_n
int elem_n
Definition: fstr_rmerge_util.h:44
help
void help(void)
Definition: fstr_rmerge.c:34
fstr_free_result
void fstr_free_result(fstr_res_info **res, int area_n)
Definition: fstr_rmerge_util.c:444
fstr_out_log
void fstr_out_log(const char *fmt,...)
Log output.
Definition: fstr_rmerge_util.c:34
fstr_res_info
Utility for reading and processing results computed in parallel.
Definition: fstr_rmerge_util.h:18
STRID_DEFAULT
#define STRID_DEFAULT
Definition: fstr_rmerge.c:23
HECMW_finalize
int HECMW_finalize(void)
Definition: hecmw_finalize.c:9
fstr_free_glmesh
void fstr_free_glmesh(struct hecmwST_local_mesh *mesh)
Delete a single region mesh.
Definition: fstr_rmerge_util.c:604
BINARY_DEFAULT
#define BINARY_DEFAULT
Definition: fstr_rmerge.c:21
intid
int intid
Definition: hecmw_res_type_conv.c:17
HECMW_get_error
int HECMW_get_error(char **errmsg)
Definition: hecmw_error.c:49
HECMW_result_io_txt_write_ST_by_fname
int HECMW_result_io_txt_write_ST_by_fname(char *filename, struct hecmwST_result_data *result, int n_node, int n_elem, char *header, char *comment)
Definition: hecmw_result_io_txt.c:694
fstr_set_log_fp
void fstr_set_log_fp(FILE *log_fp)
Set file pointer for log output.
Definition: fstr_rmerge_util.c:27
HECMW_init
int HECMW_init(int *argc, char ***argv)
Definition: hecmw_init.c:24
strid
int strid
Definition: hecmw_res_type_conv.c:15
data
CNFData data
Definition: neu_reporter.cpp:18
NRANK_DEFAULT
#define NRANK_DEFAULT
Definition: fstr_rmerge.c:22
ENDID_DEFAULT
#define ENDID_DEFAULT
Definition: fstr_rmerge.c:24
HECMW_result_io_bin_write_ST_by_fname
int HECMW_result_io_bin_write_ST_by_fname(char *filename, struct hecmwST_result_data *result, int n_node, int n_elem, char *header, char *comment)
Definition: hecmw_result_io_bin.c:654
fstr_create_glt
fstr_glt * fstr_create_glt(struct hecmwST_local_mesh **mesh, int area_n)
Create table for global ID, local ID and belonging area records fstr_glt.
Definition: fstr_rmerge_util.c:469
fstr_create_glmesh
struct hecmwST_local_mesh * fstr_create_glmesh(fstr_glt *glt)
Create a single region mesh.
Definition: fstr_rmerge_util.c:579
fstr_rmerge_util.h
fstr_glt
Table for global ID, local ID and belonging area records.
Definition: fstr_rmerge_util.h:40
fstr_glt::node_n
int node_n
Definition: fstr_rmerge_util.h:43
fstr_free_glt
void fstr_free_glt(fstr_glt *glt)
Delete fstr_glt.
Definition: fstr_rmerge_util.c:566
fstr_get_all_result
fstr_res_info ** fstr_get_all_result(char *name_ID, int step, int area_n, int refine, int nrank)
Read all area data of step.
Definition: fstr_rmerge_util.c:198
HECMW_result_init
int HECMW_result_init(struct hecmwST_local_mesh *hecMESH, int i_step, char *header, char *comment)
Definition: hecmw_result.c:56
HECMW_result_free
void HECMW_result_free(struct hecmwST_result_data *result)
Definition: hecmw_result.c:21
fstr_all_result
struct hecmwST_result_data * fstr_all_result(fstr_glt *glt, fstr_res_info **res, int refine)
Combine data in all areas of the step.
Definition: fstr_rmerge_util.c:363
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
INTID_DEFAULT
#define INTID_DEFAULT
Definition: fstr_rmerge.c:25
HECMW_FILENAME_LEN
#define HECMW_FILENAME_LEN
Definition: hecmw_config.h:72
hecmw_result_io_bin.h
set_fname
void set_fname(int argc, char **argv, char *out_fheader, int *binary, int *nrank, int *strid, int *endid, int *intid)
Definition: fstr_rmerge.c:47
hecmw_util.h
HECMW_MSG_LEN
#define HECMW_MSG_LEN
Definition: hecmw_config.h:74
HECMW_HEADER_LEN
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:68
main
int main(int argc, char **argv)
Definition: fstr_rmerge.c:133
fstr_free_mesh
void fstr_free_mesh(struct hecmwST_local_mesh **mesh, int area_n)
Delete mesh.
Definition: fstr_rmerge_util.c:145
HECMW_ctrl_get_result_fileheader
char * HECMW_ctrl_get_result_fileheader(char *name_ID, int istep, int *fg_text)
Definition: hecmw_control.c:2321