FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
neu2fstr.cpp
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 <iostream>
7 #include <stdio.h>
8 #include <string.h>
9 #include <time.h>
10 
11 #include "CConvMessage.h"
12 #include "CNFData.h"
13 #include "CFSTRData.h"
14 #include "conv_neu2hec.h"
15 #include "conv_neu2fstr_static.h"
16 #include "conv_neu2fstr_dynamic.h"
17 #include "conv_neu2fstr_heat.h"
18 
19 #define VER "1.004"
20 #define AUTHOR "The University of Tokyo, RSS21 Project"
21 
22 static int solution = sol_static;
23 static bool fg_hecmw_ctrl = false;
24 static bool fg_res_name = false;
25 static bool fg_vis_name = false;
26 static char date_time[256];
27 static char neu_name[256];
28 static char mesh_name[256];
29 static char ctrl_name[256];
30 static char hecmw_ctrl_name[256] = "hecmw_ctrl.dat";
31 static char res_name[256]; // default: mesh_name + ".res"
32 static char vis_name[256]; // default: mesh_name + ".vis"
33 static bool fg_direct = false;
34 
35 static CNFData nfdata;
36 static CFSTRData fstrdata;
37 
38 using namespace std;
39 
40 static void remove_cr(char *s) {
41  int n = strlen(s);
42 
43  if (n == 0) return;
44 
45  n--;
46 
47  if (s[n] == '\r' || s[n] == '\n') s[n] = 0;
48 }
49 
50 //-----------------------------------------------------------------------------
51 
52 void title() {
53  printf("NEU to FrontSTR Data Converter, Ver. %s \n", VER);
54  printf("Copyright (C) 2006 %s, All right reserved.\n", AUTHOR);
55 }
56 
57 //-----------------------------------------------------------------------------
58 
59 void help() {
60  printf(
61  "[usage]\n"
62  " neu2fstr ([options]) [sSeEhH] [neu] [mesh] [ctrl]\n"
63  " [options]\n"
64  " -h : show help (this message)\n"
65  " -c : create HEC-MW control file (hecmw_ctrl.dat)\n"
66  " -r [name] : write result file name in hecmw_ctrl.dat "
67  "(default:[mesh].res)\n"
68  " -v [name] : write visual file name in hecmw_ctrl.dat "
69  "(default:[mesh].vis)\n"
70  " -d : set direct solver in FrontSTR control file\n"
71  " [sSeEhH] : command character s,S,e,E,h or H (specify only one "
72  "character)\n"
73  " s,S : generate for static analysis\n"
74  " e,E : generate for eigen analysis\n"
75  " CAUTION) Edit !EIGEN section in generated [ctrl]\n"
76  " h,H : generate for heat transfer analysis\n"
77  " [neu] : neutral file name\n"
78  " [mesh] : mesh file name\n"
79  " [ctrl] : FrontSTR control file name\n");
80  fflush(stdout);
81 }
82 
83 //-----------------------------------------------------------------------------
84 
85 bool set_params(int argc, char **argv) {
86  int count = 0;
87 #define CMP(x) (strcmp(argv[i], (x)) == 0)
88 
89  for (int i = 1; i < argc; i++) {
90  if (CMP("-h")) {
91  return false;
92 
93  } else if (CMP("-c")) {
94  fg_hecmw_ctrl = true;
95 
96  } else if (CMP("-d")) {
97  fg_direct = true;
98 
99  } else if (CMP("-v")) {
100  fg_vis_name = true;
101  i++;
102 
103  if (i >= argc) {
104  fprintf(stderr, "##Error: Visual output file name is required\n");
105  return false;
106  }
107 
108  snprintf(vis_name, sizeof(vis_name), "%s", argv[i]);
109 
110  } else if (CMP("-r")) {
111  fg_res_name = true;
112  i++;
113 
114  if (i >= argc) {
115  fprintf(stderr, "##Error: Result file name is required\n");
116  return false;
117  }
118 
119  snprintf(res_name, sizeof(res_name), "%s", argv[i]);
120 
121  } else {
122  switch (count) {
123  case 0:
124  if (CMP("s") || CMP("S")) {
125  solution = sol_static;
126 
127  } else if (CMP("e") || CMP("E")) {
128  solution = sol_eigen;
129 
130  } else if (CMP("h") || CMP("H")) {
131  solution = sol_heat;
132 
133  } else {
134  fprintf(stderr, "##Error: No such solution type %s\n", argv[i]);
135  return false;
136  }
137 
138  case 1:
139  snprintf(neu_name, sizeof(neu_name), "%s", argv[i]);
140  break;
141 
142  case 2:
143  snprintf(mesh_name, sizeof(mesh_name), "%s", argv[i]);
144  break;
145 
146  case 3:
147  snprintf(ctrl_name, sizeof(ctrl_name), "%s", argv[i]);
148  break;
149 
150  default:
151  fprintf(stderr, "##Error: Too many arguments\n");
152  return false;
153  }
154 
155  count++;
156  }
157  }
158 
159 #undef CMP
160 
161  switch (count) {
162  case 0:
163  fprintf(stderr,
164  "##Error: one character, neu, mesh and ctrl file must be "
165  "specified\n");
166  return false;
167 
168  case 1:
169  fprintf(stderr, "##Error: neu, mesh and ctrl file must be specified\n");
170  return false;
171 
172  case 2:
173  fprintf(stderr, "##Error: mesh and ctrl file must be specified\n");
174  return false;
175 
176  case 3:
177  fprintf(stderr, "##Error: ctrl file must be specified\n");
178  return false;
179  }
180 
181  if (!fg_res_name) {
182  snprintf(res_name, sizeof(res_name), "%s.res", mesh_name);
183  }
184 
185  if (!fg_vis_name) {
186  snprintf(vis_name, sizeof(vis_name), "%s.vis", mesh_name);
187  }
188 
189  return true;
190 }
191 
192 //-----------------------------------------------------------------------------
193 
194 void create_comment(char *s, size_t s_size, bool fg_mesh) {
195  if (fg_mesh) {
196  snprintf(s, s_size,
197  "###########################################################\n"
198  " HEC-MW Mesh File Generated by neu2fstr ver.%s\n"
199  " Date&Time: %s\n"
200  " Original : %s\n"
201  " Control : %s\n"
202  "###########################################################\n",
203  VER, date_time, neu_name, ctrl_name);
204  } else {
205  snprintf(s, s_size,
206  "###########################################################\n"
207  " FrontSTR Control File Generated by neu2fstr ver.%s\n"
208  " Date&Time: %s\n"
209  " Original : %s\n"
210  " Mesh : %s\n"
211  "###########################################################\n",
212  VER, date_time, neu_name, mesh_name);
213  }
214 }
215 
216 //-----------------------------------------------------------------------------
217 
219  FILE *fp = fopen(hecmw_ctrl_name, "w");
220 
221  if (!fp) {
222  fprintf(stderr, "##Error: Cannot create hecmw_ctrl.dat file\n");
223  return false;
224  }
225 
226  fprintf(fp, "###########################################################\n");
227  fprintf(fp, "# HEC-MW Control File Generated by neu2fstr ver.%s\n", VER);
228  fprintf(fp, "# Date&Time: %s\n", date_time);
229  fprintf(fp, "# Original : %s\n", neu_name);
230  fprintf(fp, "###########################################################\n");
231  fprintf(fp, "!MESH, NAME=fstrMSH,TYPE=HECMW-ENTIRE\n");
232  fprintf(fp, " %s\n", mesh_name);
233  fprintf(fp, "!CONTROL,NAME=fstrCNT\n");
234  fprintf(fp, " %s\n", ctrl_name);
235  fprintf(fp, "!RESULT,NAME=fstrRES,IO=OUT\n");
236  fprintf(fp, " %s\n", res_name);
237  fprintf(fp, "!RESULT,NAME=vis_out,IO=OUT\n");
238  fprintf(fp, " %s\n", vis_name);
239  fclose(fp);
240  return true;
241 }
242 
243 //-----------------------------------------------------------------------------
244 
245 void set_solution(CFSTRData &fstrdata, int solution) {
246  CFSTRDB_Solution *sol = new CFSTRDB_Solution();
247 
248  switch (solution) {
249  case sol_static:
251  break;
252 
253  case sol_eigen:
255  break;
256 
257  case sol_heat:
259  break;
260 
261  default:
262  assert(0);
263  }
264 
265  fstrdata.DB.push_back(sol);
266 }
267 
268 //-----------------------------------------------------------------------------
269 
270 void set_solver(CFSTRData &fstrdata, bool fg_direct = false) {
271  CFSTRDB_Solver *solver = new CFSTRDB_Solver();
272 
273  if (fg_direct) {
274  snprintf(solver->method, sizeof(solver->method), "DIRECT");
275 
276  } else {
277  snprintf(solver->method, sizeof(solver->method), "CG");
278  }
279 
280  fstrdata.DB.push_back(solver);
281 }
282 
283 //-----------------------------------------------------------------------------
284 // main
285 //-----------------------------------------------------------------------------
286 
287 int main(int argc, char **argv) {
288  time_t t;
289  time(&t);
290  snprintf(date_time, sizeof(date_time), "%s", ctime(&t));
291  remove_cr(date_time);
292  title();
293 
294  if (!set_params(argc, argv)) {
295  fflush(stderr);
296  help();
297  return -1;
298  }
299 
300  cout << "loading neu file ... " << endl;
301 
302  try {
303  nfdata.Load(neu_name);
304 
305  } catch (CNFError e) {
306  fprintf(stderr, "NEU loading error : %s\n", e.Msg());
307  return -1;
308  }
309 
310  try {
311  cout << "converting to HEC-MW mesh ... " << endl;
312  conv_neu2hec(nfdata, fstrdata, solution);
313  cout << "converting to FrontSTR control data " << endl;
314  conv_neu2fstr_static(nfdata, fstrdata);
315  conv_neu2fstr_dynamic(nfdata, fstrdata);
316  conv_neu2fstr_heat(nfdata, fstrdata);
317  set_solution(fstrdata, solution);
318  set_solver(fstrdata, fg_direct);
319 
320  } catch (CConvMessage &e) {
321  fprintf(stderr, "Converting error : %s\n", e.Msg());
322  return -1;
323  }
324 
325  char comment[256];
326  cout << "saving HEC-MW mesh file..." << endl;
327  create_comment(comment, sizeof(comment), true);
328 
329  if (!fstrdata.SaveMesh(mesh_name, comment)) {
330  fprintf(stderr, "Cannot save mesh data\n");
331  return -1;
332  }
333 
334  cout << "saving FrontSTR control file..." << endl;
335  create_comment(comment, sizeof(comment), false);
336 
337  if (!fstrdata.SaveCtrl(ctrl_name, comment)) {
338  fprintf(stderr, "Cannot save control data\n");
339  return -1;
340  }
341 
342  if (fg_hecmw_ctrl) {
343  cout << "creating hecmw_ctrl.dat file..." << endl;
344 
345  if (!create_hecmw_ctrl()) return -1;
346  }
347 
348  cout << "neu2fstr completed." << endl;
349  return 0;
350 }
char method[30]
Definition: CFSTRDB.h:84
virtual bool SaveCtrl(const char *file_name, const char *comment="")
Definition: CFSTRData.cpp:44
virtual bool SaveMesh(const char *file_name, const char *comment="")
Definition: CFSTRData.cpp:21
std::vector< CHECDataBlock * > DB
Definition: CHECData.h:30
virtual void Load(const char *fname)
Definition: CNFData.cpp:66
virtual const char * Msg()
Definition: CNFMessage.cpp:21
bool conv_neu2fstr_dynamic(CNFData &neu, CHECData &hec)
void conv_neu2fstr_heat(CNFData &neu, CHECData &hec)
void conv_neu2fstr_static(CNFData &neu, CHECData &hec)
void conv_neu2hec(CNFData &neu, CHECData &hec, int sol)
@ sol_eigen
Definition: conv_neu2hec.h:18
@ sol_heat
Definition: conv_neu2hec.h:18
@ sol_static
Definition: conv_neu2hec.h:18
int main(int argc, char **argv)
Definition: neu2fstr.cpp:287
void title()
Definition: neu2fstr.cpp:52
#define AUTHOR
Definition: neu2fstr.cpp:20
bool set_params(int argc, char **argv)
Definition: neu2fstr.cpp:85
void help()
Definition: neu2fstr.cpp:59
#define VER
Definition: neu2fstr.cpp:19
#define CMP(x)
void set_solver(CFSTRData &fstrdata, bool fg_direct=false)
Definition: neu2fstr.cpp:270
bool create_hecmw_ctrl()
Definition: neu2fstr.cpp:218
void create_comment(char *s, size_t s_size, bool fg_mesh)
Definition: neu2fstr.cpp:194
void set_solution(CFSTRData &fstrdata, int solution)
Definition: neu2fstr.cpp:245