FrontISTR  5.9.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, size_t out_fheader_len,
48  int* binary, 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  snprintf(out_fheader, out_fheader_len, "%s", 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, sizeof(out_fheader), &binary, &nrank,
160  &strid, &endid, &intid);
161  fstr_out_log("out file name header is %s\n", out_fheader);
162 
163  mesh = fstr_get_all_local_mesh("fstrMSH", nrank, &area_n, &refine);
164  if (!mesh) error_stop();
165 
166  fstr_out_log("table creating .. \n");
167  glt = fstr_create_glt(mesh, area_n);
168  if (!glt) {
169  fprintf(stderr, "ERROR : Cannot create global_local table.\n");
170  fstr_free_mesh(mesh, area_n);
171  exit(-1);
172  }
173 
174  glmesh = fstr_create_glmesh(glt);
175  if (!glmesh) {
176  fprintf(stderr, "ERROR : Cannot create global table.\n");
177  fstr_free_mesh(mesh, area_n);
178  fstr_free_glt(glt);
179  exit(-1);
180  }
181 
182  fstr_free_mesh(mesh, area_n);
183 
184  step_n = (endid > -1) ? endid : fstr_get_step_n("fstrRES", nrank);
185 
186  for (step = strid; step <= step_n; step++) {
187  if ((step % intid) != 0 && step != step_n) continue;
188 
189  fstr_out_log("step:%d .. reading .. ", step);
190  res = fstr_get_all_result("fstrRES", step, area_n, refine, nrank);
191  if (!res) {
192  fstr_free_result(res, area_n);
193  continue;
194  }
195  fstr_out_log("end\n");
196 
197  fstr_out_log("step:%d .. combining .. ", step);
198  data = fstr_all_result(glt, res, refine);
199  if (!data) {
200  fprintf(stderr, "ERROR : Cannot combine result structure.\n");
201  fstr_free_glt(glt);
202  fstr_free_glmesh(glmesh);
203  fstr_free_result(res, area_n);
204  exit(-1);
205  }
206  fstr_out_log("end\n");
207 
208  if (nrank == 0) {
209  if ((fileheader = HECMW_ctrl_get_result_fileheader("fstrRES", step,
210  &fg_text)) == NULL)
211  return 0;
212  } else {
213  if ((fileheader = HECMW_ctrl_get_result_fileheader_sub(
214  "fstrRES", step, nrank, 0, &fg_text)) == NULL)
215  return 0;
216  }
217  strcpy(buff, fileheader);
218  strcpy(dirname, "");
219  ptoken = strtok(buff, "/");
220  ntoken = strtok(NULL, "/");
221  while (ntoken) {
222  strcat(dirname, ptoken);
223  strcat(dirname, "/");
224  ptoken = ntoken;
225  ntoken = strtok(NULL, "/");
226  }
227  snprintf(out_fname, sizeof(out_fname), "%s%s.%d", dirname, out_fheader, step);
228  fstr_out_log("output to %s .. ", out_fname);
229  HECMW_result_get_header(header, sizeof(header));
230  HECMW_result_get_comment(comment, sizeof(comment));
231  HECMW_result_init(glmesh, step, header, comment);
232  if (binary) {
233  rcode = HECMW_result_io_bin_write_ST_by_fname(out_fname, data, glt->node_n,
234  glt->elem_n, header, comment);
235  } else {
236  rcode = HECMW_result_io_txt_write_ST_by_fname(out_fname, data, glt->node_n,
237  glt->elem_n, header, comment);
238  }
239  if (rcode) {
240  fprintf(stderr, "ERROR : Cannot open/write file %s\n", out_fname);
241  fstr_free_glt(glt);
242  fstr_free_glmesh(glmesh);
243  fstr_free_result(res, area_n);
245  exit(-1);
246  }
247  fstr_out_log("end\n");
248 
249  fstr_free_result(res, area_n);
251  }
252 
253  fstr_free_glt(glt);
254  fstr_free_glmesh(glmesh);
255 
256  HECMW_finalize();
257 
258  exit(0);
259 }
void help(void)
Definition: fstr_rmerge.c:34
#define INTID_DEFAULT
Definition: fstr_rmerge.c:25
int main(int argc, char **argv)
Definition: fstr_rmerge.c:133
#define NRANK_DEFAULT
Definition: fstr_rmerge.c:22
void set_fname(int argc, char **argv, char *out_fheader, size_t out_fheader_len, int *binary, int *nrank, int *strid, int *endid, int *intid)
Definition: fstr_rmerge.c:47
#define ENDID_DEFAULT
Definition: fstr_rmerge.c:24
#define STRID_DEFAULT
Definition: fstr_rmerge.c:23
void error_stop(void)
Definition: fstr_rmerge.c:27
#define BINARY_DEFAULT
Definition: fstr_rmerge.c:21
struct hecmwST_local_mesh * fstr_create_glmesh(fstr_glt *glt)
Create a single region mesh.
struct hecmwST_local_mesh ** fstr_get_all_local_mesh(char *name_ID, int nrank, int *area_number, int *refine)
Read all distributed meshes.
void fstr_set_log_fp(FILE *log_fp)
Set file pointer for log output.
void fstr_free_glmesh(struct hecmwST_local_mesh *mesh)
Delete a single region mesh.
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.
void fstr_free_glt(fstr_glt *glt)
Delete fstr_glt.
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.
void fstr_out_log(const char *fmt,...)
Log output.
int fstr_get_step_n(char *name_ID, int nrank)
Check the number of steps (check for the existence of files)
struct hecmwST_result_data * fstr_all_result(fstr_glt *glt, fstr_res_info **res, int refine)
Combine data in all areas of the step.
void fstr_free_result(fstr_res_info **res, int area_n)
void fstr_free_mesh(struct hecmwST_local_mesh **mesh, int area_n)
Delete mesh.
#define HECMW_FILENAME_LEN
Definition: hecmw_config.h:74
#define HECMW_MSG_LEN
Definition: hecmw_config.h:76
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:70
char * HECMW_ctrl_get_result_fileheader_sub(char *name_ID, int istep, int n_rank, int i_rank, int *fg_text)
char * HECMW_ctrl_get_result_fileheader(char *name_ID, int istep, int *fg_text)
struct hecmwST_local_mesh * mesh
Definition: hecmw_repart.h:71
int HECMW_get_error(char **errmsg)
Definition: hecmw_error.c:45
int HECMW_finalize(void)
Definition: hecmw_finalize.c:9
int HECMW_init(int *argc, char ***argv)
Definition: hecmw_init.c:24
#define NULL
int endid
int intid
int strid
char * HECMW_result_get_header(char *buff, size_t buff_size)
Definition: hecmw_result.c:219
int HECMW_result_init(struct hecmwST_local_mesh *hecMESH, int i_step, char *header, char *comment)
Definition: hecmw_result.c:56
void HECMW_result_free(struct hecmwST_result_data *result)
Definition: hecmw_result.c:21
char * HECMW_result_get_comment(char *buff, size_t buff_size)
Definition: hecmw_result.c:224
int HECMW_result_io_bin_write_ST_by_fname(char *filename, struct hecmwST_result_data *result, int n_node, int n_elem, char *header, char *comment)
int HECMW_result_io_txt_write_ST_by_fname(char *filename, struct hecmwST_result_data *result, int n_node, int n_elem, char *header, char *comment)
CNFData data
Table for global ID, local ID and belonging area records.
Utility for reading and processing results computed in parallel.