Debian Codegraph @

packages/seacas/applications/nem_spread/pe_input.C

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8 /*--------------------------------------------------------------------------*/
9 /* Purpose: Determine file types for command files and read in the parallel */
10 /* ExodusII command file. */
11 /*--------------------------------------------------------------------------*/
12
13 #include "copy_string_cpp.h"
14 #include "fmt/ostream.h"
15 #include "nem_spread.h"
16 #include "ps_pario_const.h" // for PIO_Info, Parallel_IO, etc
17 #include "rf_allo.h" // for array_alloc
18 #include "rf_io_const.h" // for ExoFile, etc
19 #include "scopeguard.h"
20 #include <cstdio> // for nullptr, stderr, etc
21 #include <cstdlib> // for exit, realloc
22 #include <cstring> // for strtok, strchr, strstr, etc
23
24 #define TLIST_CNT 5
25
26 constexpr int MAX_INPUT_STR_LN = 4096; /* maximum string length for read_string() */
27
28 /*****************************************************************************/
29 int read_mesh_file_name(const char *filename)
30 {
31 /* local declarations */
32 FILE *file_cmd = nullptr;
33 char inp_line[MAX_INPUT_STR_LN + 1];
34 char inp_copy[MAX_INPUT_STR_LN + 1];
35
36 /* Open the file */
37 if ((file_cmd = fopen(filename, "r")) == nullptr) {
38 return -1;
39 }
40 ON_BLOCK_EXIT(fclose, file_cmd);
41
42 /* Begin parsing the input file */
43 while (fgets(inp_line, MAX_INPUT_STR_LN, file_cmd) != nullptr) {
44 /* skip any line that is a comment */
45 if ((inp_line[0] != '#') && (inp_line[0] != '\n')) {
46
47 copy_string(inp_copy, inp_line);
48 clean_string(inp_line, " \t");
49 char *cptr = strtok(inp_line, "\t=");
50 /****** The input ExodusII file name ******/
51 if (token_compare(cptr, "input fem file") != 0) {
52 if (ExoFile.empty()) {
53 cptr = strtok(nullptr, "\t=");
54 strip_string(cptr, " \t\n");
55 ExoFile = std::string(cptr);
56 break;
57 }
58 }
59 }
60 }
61 return 0;
62 }
63
64 /*****************************************************************************/
65
66 template <typename T, typename INT>
67 int read_pexoII_info(NemSpread<T, INT> &spreader, const char *filename)
68
69 /*
70 * This function reads the ASCII parallel-exodus command file.
71 *
72 * Input
73 * -----
74 * filename - The name of the command file.
75 */
76 {
77 /* local declarations */
78 FILE *file_cmd = nullptr;
79 char inp_line[MAX_INPUT_STR_LN + 1];
80 char inp_copy[MAX_INPUT_STR_LN + 1];
81 char *cptr;
82 char *cptr2;
83 char *cptr3;
84 int i;
85 int icnt;
86 int tlist_alloc;
87
88 /***************************** BEGIN EXECUTION ******************************/
89
90 /* Open the file */
91 if ((file_cmd = fopen(filename, "r")) == nullptr) {
92 return -1;
93 }
94 ON_BLOCK_EXIT(fclose, file_cmd);
95
96 /* Begin parsing the input file */
97 while (fgets(inp_line, MAX_INPUT_STR_LN, file_cmd)) {
98 /* skip any line that is a comment */
99 if ((inp_line[0] != '#') && (inp_line[0] != '\n')) {
100
101 copy_string(inp_copy, inp_line);
102 clean_string(inp_line, " \t");
103 cptr = strtok(inp_line, "\t=");
104 /****** The input ExodusII file name ******/
105 if (token_compare(cptr, "input fem file")) {
106 if (ExoFile.empty()) {
107 cptr = strtok(nullptr, "\t=");
108 strip_string(cptr, " \t\n");
109 ExoFile = std::string(cptr);
110 }
111 }
112 /****** The input NemesisI load balance file name ******/
113 else if (token_compare(cptr, "lb file")) {
114 if (Exo_LB_File.empty()) {
115 cptr = strtok(nullptr, "\t=");
116 strip_string(cptr, " \t\n");
117 Exo_LB_File = std::string(cptr);
118 }
119 }
120 /****** The scalar results ExodusII file name ******/
121 else if (token_compare(cptr, "scalar results fem file")) {
122 if (Exo_Res_File.empty()) {
123 cptr = strtok(nullptr, "\t=");
124 strip_string(cptr, " \t\n");
125 Exo_Res_File = std::string(cptr);
126 }
127 }
128 /****** The parallel results ExodusII file name ******/
129 else if (token_compare(cptr, "parallel results file base name")) {
130 if (Output_File_Base_Name.empty()) {
131 cptr = strtok(nullptr, "\t=");
132 strip_string(cptr, " \t\n");
133 Output_File_Base_Name = std::string(cptr);
134 }
135 }
136 /****** The Number of Processors ******/
137 else if (token_compare(cptr, "number of processors")) {
138 if (spreader.Proc_Info[0] < 0) {
139 cptr = strtok(nullptr, "\t=");
140 strip_string(cptr, " \t\n");
141 if (sscanf(cptr, "%d", &(spreader.Proc_Info[0])) != 1) {
142 fmt::print(stderr,
143 "{}: ERROR, can\'t interpret int for number of"
144 " Processors.\n",
145 __func__);
146 exit(1);
147 }
148 }
149 }
150 /****** The File extension to use for spread files ******/
151 else if (token_compare(cptr, "file extension for spread files")) {
152 cptr = strtok(nullptr, "\t=");
153 strip_string(cptr, " \t\n");
154 PIO_Info.Exo_Extension = std::string(cptr);
155 }
156
157 /****** Is There a Scalar Mesh File to Use ******/
158 else if (token_compare(cptr, "use scalar mesh file")) {
159 if (Gen_Flag < 0) {
160 cptr = strtok(nullptr, "\t=");
161 strip_string(cptr, " \t\n");
162 if (Gen_Flag < 0) {
163 if (token_compare(cptr, "yes")) {
164 Gen_Flag = 1;
165 }
166 else {
167 Gen_Flag = 0;
168 }
169 }
170 }
171 }
172 /****** The Debug reporting level ******/
173 else if (token_compare(cptr, "debug")) {
174 if (Debug_Flag < 0) {
175 cptr = strtok(nullptr, "\t=");
176 strip_string(cptr, " \t\n");
177 if (sscanf(cptr, "%d", &Debug_Flag) != 1) {
178 fmt::print(stderr, "{}: ERROR, can\'t interpret int for Debug_Flag\n", __func__);
179 exit(1);
180 }
181 }
182 }
183 /****** Restart Time List ******/
184 else if (token_compare(cptr, "restart info")) {
185
186 cptr = strchr(cptr, '\0');
187 cptr++;
188
189 /*
190 * If there is a list, then need to change the comma's in
191 * the list to blank spaces so that the strtok below works
192 * correctly. So, search for commas between the beginning
193 * delimiter, "{", and the end delimiter, "}", and change
194 * them to blank spaces.
195 */
196 cptr2 = strchr(cptr, '{');
197 if (cptr2 != nullptr) {
198 icnt = strlen(cptr2);
199 for (i = 0; i < icnt; i++) {
200 if (*cptr2 == '}') {
201 break;
202 }
203 if (*cptr2 == ',') {
204 *cptr2 = ' ';
205 }
206 cptr2++;
207 }
208 }
209
210 strip_string(cptr, " \t\n=");
211 cptr = strtok(cptr, ",");
212
213 /* Loop until all the suboptions have been specified */
214 while (cptr != nullptr) {
215 strip_string(cptr, " \t\n");
216 string_to_lower(cptr, '\0');
217
218 /* check if the user wants to specifically turn this off */
219 if (strcmp(cptr, "off") == 0) {
220 if (spreader.Restart_Info.Flag < 0) {
221 spreader.Restart_Info.Flag = 0;
222 spreader.Restart_Info.Num_Times = 0;
223 }
224 }
225 /* check if the user wants all of the time steps */
226 else if (strcmp(cptr, "all") == 0) {
227 if (spreader.Restart_Info.Flag < 0) {
228 spreader.Restart_Info.Flag = 1;
229 spreader.Restart_Info.Num_Times = -1; /* -1 designates read all times */
230 }
231 }
232 /* IGNORED check if the user wants move variable in blocks IGNORED */
233 else if (strstr(cptr, "block")) {
234 cptr2 = strchr(cptr, '=');
235 if (cptr2 == nullptr) {
236 fmt::print(stderr, "fatal: must specify a value with \"block\"");
237 exit(1);
238 }
239 cptr2++;
240 }
241 /* check if the user has a list of time indices to get */
242 else if (strstr(cptr, "list")) {
243 /* "{" defines the beginning of the group designator */
244 cptr2 = strchr(cptr, '{');
245 if (cptr2 == nullptr) {
246 fmt::print(stderr, fmt::runtime("fatal: list start designator \"{\" not found"));
247 exit(1);
248 }
249 cptr2++;
250 cptr3 = strchr(cptr, '}');
251 if (cptr3 == nullptr) {
252 fmt::print(stderr, fmt::runtime("fatal: list end designator \"}\" not found"));
253 exit(1);
254 }
255 *cptr3 = '\0';
256
257 /* Allocate the time list */
258 spreader.Restart_Info.Time_Idx.resize(TLIST_CNT);
259 tlist_alloc = TLIST_CNT;
260 spreader.Restart_Info.Num_Times = 0;
261
262 while (cptr2) {
263 /* first check to see if they want to get the last time index */
264 if (strncmp(cptr2, "last", 4) == 0) {
265 icnt = 0;
266 spreader.Restart_Info.Time_Idx[spreader.Restart_Info.Num_Times] = 0;
267 }
268 else {
269 icnt = sscanf(cptr2, "%d",
270 &(spreader.Restart_Info.Time_Idx[spreader.Restart_Info.Num_Times]));
271 }
272
273 if (icnt >= 0) {
274 (spreader.Restart_Info.Num_Times)++;
275 }
276
277 if (spreader.Restart_Info.Num_Times >= tlist_alloc) {
278 tlist_alloc += TLIST_CNT;
279 spreader.Restart_Info.Time_Idx.resize(tlist_alloc);
280 }
281 /* move to the next blank space */
282 cptr3 = strchr(cptr2, ' ');
283 if (cptr3) {
284 /* find the next non-blank space */
285 while (*cptr3 == ' ') {
286 cptr3++;
287 }
288 }
289 cptr2 = cptr3;
290 }
291 }
292 else {
293 fmt::print(stderr, "warning: unknown restart info suboption {}", cptr);
294 exit(1);
295 }
296 cptr = strtok(nullptr, ",");
297 }
298 } /* End "if (token_compare(cptr, "restart time list"))" */
299 /****** Reserved Space for Variables ******/
300 else if (token_compare(cptr, "reserve space")) {
301
302 cptr = strchr(cptr, '\0');
303 cptr++;
304 strip_string(cptr, " \t\n=");
305 cptr = strtok(cptr, ",");
306
307 while (cptr != nullptr) {
308 strip_string(cptr, " \t\n");
309 string_to_lower(cptr, '=');
310 if (strstr(cptr, "nodal")) {
311 cptr2 = strchr(cptr, '=');
312 if (cptr2 == nullptr) {
313 fmt::print(stderr, "Error: integer value must be specified for"
314 " reserve space.\n");
315 return 0;
316 }
317 cptr2++;
318 icnt = sscanf(cptr2, "%d", &Num_Nod_Var);
319 if ((icnt <= 0) || (Num_Nod_Var < 0)) {
320 fmt::print(stderr, "Error: Invalid value for nodal variable\n");
321 return 0;
322 }
323 }
324 else if (strstr(cptr, "elemental")) {
325 cptr2 = strchr(cptr, '=');
326 if (cptr2 == nullptr) {
327 fmt::print(stderr, "Error: integer value must be specified for"
328 " reserve space.\n");
329 return 0;
330 }
331 cptr2++;
332 icnt = sscanf(cptr2, "%d", &Num_Elem_Var);
333 if ((icnt <= 0) || (Num_Elem_Var < 0)) {
334 fmt::print(stderr, "Error: Invalid value for elemental variable\n");
335 return 0;
336 }
337 }
338 else if (strstr(cptr, "global")) {
339 cptr2 = strchr(cptr, '=');
340 if (cptr2 == nullptr) {
341 fmt::print(stderr, "Error: integer value must be specified for"
342 " reserve space.\n");
343 return 0;
344 }
345 cptr2++;
346 icnt = sscanf(cptr2, "%d", &Num_Glob_Var);
347 if ((icnt <= 0) || (Num_Glob_Var < 0)) {
348 fmt::print(stderr, "Error: Invalid value for global variable\n");
349 return 0;
350 }
351 }
352 else if (strstr(cptr, "nodeset")) {
353 cptr2 = strchr(cptr, '=');
354 if (cptr2 == nullptr) {
355 fmt::print(stderr, "Error: integer value must be specified for"
356 " reserve space.\n");
357 return 0;
358 }
359 cptr2++;
360 icnt = sscanf(cptr2, "%d", &Num_Nset_Var);
361 if ((icnt <= 0) || (Num_Nset_Var < 0)) {
362 fmt::print(stderr, "Error: Invalid value for nodeset variable\n");
363 return 0;
364 }
365 }
366 else if (strstr(cptr, "sideset")) {
367 cptr2 = strchr(cptr, '=');
368 if (cptr2 == nullptr) {
369 fmt::print(stderr, "Error: integer value must be specified for"
370 " reserve space.\n");
371 return 0;
372 }
373 cptr2++;
374 icnt = sscanf(cptr2, "%d", &Num_Sset_Var);
375 if ((icnt <= 0) || (Num_Sset_Var < 0)) {
376 fmt::print(stderr, "Error: Invalid value for sideset variable\n");
377 return 0;
378 }
379 }
380
381 cptr = strtok(nullptr, ",");
382
383 } /* End "while (cptr != nullptr)" */
384 } /* End "else if (token_compare(cptr, "reserve space"))" */
385 /****** Parallel Disk Information ******/
386 else if (token_compare(cptr, "parallel disk info")) {
387
388 cptr = strchr(cptr, '\0');
389 cptr++;
390 strip_string(cptr, " \t\n=");
391 cptr = strtok(cptr, ",");
392 strip_string(cptr, " \t\n");
393 string_to_lower(cptr, '=');
394
395 /* the first sub-option must be "number" */
396 if (!strstr(cptr, "number")) {
397 fmt::print(stderr, "Error: First sup-option for disk info must be "
398 "\"number\"\n");
399 return 0;
400 }
401
402 cptr2 = strchr(cptr, '=');
403 if (cptr2 == nullptr) {
404 fmt::print(stderr, "Error: integer value must be specified for"
405 " reserve space.\n");
406 return 0;
407 }
408 cptr2++;
409 icnt = sscanf(cptr2, "%d", &(PIO_Info.Num_Dsk_Ctrlrs));
410 if ((icnt <= 0) || (PIO_Info.Num_Dsk_Ctrlrs <= 0)) {
411 fmt::print(stderr, "Error: Invalid value for # of raid controllers\n");
412 return 0;
413 }
414
415 cptr = strtok(nullptr, ",");
416 while (cptr != nullptr) {
417 strip_string(cptr, " \t\n");
418 string_to_lower(cptr, '=');
419 if (strstr(cptr, "list")) {
420 /*
421 * So, "number" references the length of the list, and
422 * I need to do some shuffling to make the new form
423 * work with the old code.
424 */
425 PIO_Info.Dsk_List_Cnt = PIO_Info.Num_Dsk_Ctrlrs;
426 PIO_Info.Num_Dsk_Ctrlrs = 0;
427
428 /* "{" defines the beginning of the list */
429 cptr = strchr(cptr, '{');
430 if (cptr == nullptr) {
431 fmt::print(stderr, "Error: disk list must be specified\n");
432 return 0;
433 }
434 cptr++;
435
436 /* allocate memory for to hold the values */
437 PIO_Info.Dsk_List = reinterpret_cast<int *>(
438 array_alloc(__FILE__, __LINE__, 1, PIO_Info.Dsk_List_Cnt, sizeof(int)));
439 for (i = 0; i < (PIO_Info.Dsk_List_Cnt - 1); i++) {
440 sscanf(cptr, "%d", &(PIO_Info.Dsk_List[i]));
441 cptr = strtok(nullptr, ", \t;");
442 }
443 /* last one is a special case */
444 sscanf(cptr, "%d}", &(PIO_Info.Dsk_List[i]));
445 }
446 else if (strstr(cptr, "offset")) {
447 cptr2 = strchr(cptr, '=');
448 if (cptr2 == nullptr) {
449 fmt::print(stderr, "Error: value must be specified with the "
450 "\"offset\" option.\n");
451 return 0;
452 }
453 cptr2++;
454 icnt = sscanf(cptr2, "%d", &(PIO_Info.PDsk_Add_Fact));
455 if ((icnt <= 0) || (PIO_Info.PDsk_Add_Fact < 0)) {
456 fmt::print(stderr, "Error: Invalid value for offset\n");
457 return 0;
458 }
459 }
460 else if (strstr(cptr, "zeros")) {
461 PIO_Info.Zeros = 1;
462 }
463 else if (strstr(cptr, "nosubdirectory")) {
464 PIO_Info.NoSubdirectory = 1;
465 }
466 else if (strstr(cptr, "stage_off")) {
467 PIO_Info.Staged_Writes = false;
468 }
469 else if (strstr(cptr, "stage_on")) {
470 PIO_Info.Staged_Writes = true;
471 }
472
473 cptr = strtok(nullptr, ",");
474 }
475 } /* End "else if (token_compare(cptr, "parallel disk info"))" */
476 else if (token_compare(cptr, "parallel file location")) {
477 cptr = strchr(cptr, '\0');
478 cptr++;
479 strip_string(cptr, " \t\n=");
480 cptr = strtok(cptr, ",");
481
482 while (cptr != nullptr) {
483 strip_string(cptr, " \t\n");
484 string_to_lower(cptr, '=');
485 if (strstr(cptr, "root")) {
486 cptr2 = strchr(cptr, '=');
487 if (cptr2 == nullptr) {
488 fmt::print(stderr, "fatal: must specify a path with \"root\"");
489 return 0;
490 }
491 cptr2++;
492 if (strlen(cptr2) == 0) {
493 fmt::print(stderr, "fatal: invalid path name specified with"
494 " \"root\"");
495 return 0;
496 }
497 PIO_Info.Par_Dsk_Root = std::string(cptr2);
498 }
499 if (strstr(cptr, "subdir")) {
500 cptr2 = strchr(cptr, '=');
501 if (cptr2 == nullptr) {
502 fmt::print(stderr, "fatal: must specify a path with \"subdir\"");
503 return 0;
504 }
505 cptr2++;
506 if (strlen(cptr2) == 0) {
507 fmt::print(stderr, "fatal: invalid path name specified with"
508 " \"subdir\"");
509 return 0;
510 }
511 PIO_Info.Par_Dsk_SubDirec = std::string(cptr2);
512 if (PIO_Info.Par_Dsk_SubDirec.back() != '/') {
513 PIO_Info.Par_Dsk_SubDirec += '/';
514 }
515 }
516
517 cptr = strtok(nullptr, ",");
518 }
519 }
520
521 } /* End "if(inp_line[0] != '#')" */
522 } /* End "while(fgets(inp_line, MAX_INPUT_STR_LN, file_cmd))" */
523
524 if (Output_File_Base_Name.empty() && !Exo_LB_File.empty()) {
525 // User did not specify a base name. Use the basenmae of the
526 // Exo_LB_File instead.
527 Output_File_Base_Name = Exo_LB_File;
528
529 // If there is an extension, strip it off...
530 size_t found = Output_File_Base_Name.find_last_of('.');
531 if (found != std::string::npos) {
532 Output_File_Base_Name = Output_File_Base_Name.substr(0, found);
533 }
534 }
535 return 0;
536 }
537
538 template int read_pexoII_info(NemSpread<float, int> &spreader, const char *filename);
539 template int read_pexoII_info(NemSpread<double, int> &spreader, const char *filename);
540
541 template int read_pexoII_info(NemSpread<double, int64_t> &spreader, const char *filename);
542 template int read_pexoII_info(NemSpread<float, int64_t> &spreader, const char *filename);