Debian Codegraph @

packages/seacas/applications/nem_slice/elb_inp.C

raw
1 /*
2 * Copyright(C) 1999-2025 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 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 *----------------------------------------------------------------------------
11 * Functions contained in this file:
12 * cmd_line_arg_parse()
13 * read_cmd_file()
14 * check_inp_specs()
15 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
16 #include "copy_string_cpp.h"
17 #include "elb.h" // for Problem_Description, etc
18 #include "elb_err.h" // for Gen_Error, error_lev
19 #include "elb_inp.h"
20 #include "elb_util.h" // for strip_string, token_compare, etc
21 #include "fmt/ostream.h"
22 #include "vector_data.h"
23 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
24 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
25 #include "XGetopt.h"
26 #include <unistd.h>
27 #else
28 #include "getopt.h" // for getopt
29 #endif
30 #include "scopeguard.h"
31 #include <cstddef> // for size_t
32 #ifndef _XOPEN_SOURCE
33 #define _XOPEN_SOURCE 500
34 #endif
35 #include <cstdlib> // for malloc, exit, free
36 #include <cstring> // for strcmp, strstr, strchr, etc
37 #include <exodusII.h> // for ex_close, EX_READ, etc
38
39 namespace {
40 int my_getsubopt(char **optionp, char *const *tokens, char **valuep);
41
42 void print_usage();
43
44 std::string remove_extension(const std::string &filename)
45 {
46 // Strip off the extension
47 size_t ind = filename.find_last_of('.', filename.size());
48 if (ind != std::string::npos) {
49 return filename.substr(0, ind);
50 }
51 return filename;
52 }
53
54 int check_change_sets(int exoid, int &selected_change_set)
55 {
56 // First, see if the file contains change sets...
57 int num_change_sets = ex_inquire_int(exoid, EX_INQ_NUM_CHILD_GROUPS);
58
59 if (num_change_sets == 0) {
60 if (selected_change_set > 0) {
61 fmt::print(stderr,
62 "ERROR: Reading from change set {} was specified, but the file contains no "
63 "change sets.\n",
64 selected_change_set);
65
66 Gen_Error(0, "fatal: no change sets in mesh database");
67 return 0;
68 }
69 return exoid;
70 }
71
72 // File contains change sets...
73 if (selected_change_set == 0) {
74 fmt::print(
75 stderr,
76 "\nWARNING: Exodus database contains {} change sets.\n Setting to read from "
77 "first change set since `-change_set #` option not specified.\n\n",
78 num_change_sets);
79 selected_change_set = 1;
80 return exoid + selected_change_set;
81 }
82
83 if (selected_change_set > num_change_sets) {
84 fmt::print(stderr,
85 "ERROR: Change set {} was specified for reading, but mesh only contains {} change "
86 "sets.\n",
87 selected_change_set, num_change_sets);
88 Gen_Error(0, "fatal: selected change set does not exist in mesh database");
89 return 0;
90 }
91 // Contains change sets and selected change set is in range...
92 fmt::print(stderr, "NOTE: Mesh data will be read from change set {} of {}\n",
93 selected_change_set, num_change_sets);
94 return exoid + selected_change_set;
95 }
96 } // namespace
97 /*****************************************************************************/
98 /*****************************************************************************/
99 /*****************************************************************************/
100 /* This function parses the command line options and stores the information
101 * in the appropriate data locations.
102 *---------------------------------------------------------------------------*/
103 template int cmd_line_arg_parse(int argc, char *argv[], std::string &exoII_inp_file,
104 std::string &ascii_inp_file, std::string &nemI_out_file,
105 Machine_Description *machine, LB_Description<int> *lb,
106 Problem_Description *prob, Solver_Description *solver,
107 Weight_Description *weight);
108
109 template int cmd_line_arg_parse(int argc, char *argv[], std::string &exoII_inp_file,
110 std::string &ascii_inp_file, std::string &nemI_out_file,
111 Machine_Description *machine, LB_Description<int64_t> *lb,
112 Problem_Description *prob, Solver_Description *solver,
113 Weight_Description *weight);
114
115 template <typename INT>
116 int cmd_line_arg_parse(int argc, char *argv[], /* Args as passed by main() */
117 std::string &exoII_inp_file, /* The input ExodusII file name */
118 std::string &ascii_inp_file, /* The ASCII input file name */
119 std::string &nemI_out_file, /* Output NemesisI file name */
120 Machine_Description *machine, /* Structure for machine description */
121 LB_Description<INT> *lb, /* Structure for load balance description */
122 Problem_Description *prob, /* Structure for various problem params */
123 Solver_Description *solver, /* Structure for eigen solver params */
124 Weight_Description *weight /* Structure for weighting graph */
125 )
126 {
127 int opt_let;
128 int iret;
129 int el_blk;
130 int wgt;
131 int max_dim = 0;
132 int i;
133 char *sub_opt = nullptr, *value = nullptr, *cptr = nullptr, *cptr2 = nullptr;
134 std::string ctemp;
135
136 /* see NOTE in elb.h about the order of the following array */
137 const char *weight_subopts[] = {"none", "read", "eb", "var_index",
138 "edges", "time_index", "var_name", nullptr};
139
140 const char *mach_subopts[] = {"invalid", "mesh", "hcube", "hypercube", "cluster", nullptr};
141
142 const char *lb_subopts[] = {"invalid", "multikl", "spectral", "inertial", "linear",
143 "random", "scattered", "infile", "kl", "none",
144 "num_sects", "cnctd_dom", "outfile", "zpinch", "brick",
145 "rcb", "rib", "hsfc", "ignore_z", nullptr};
146
147 const char *solve_subopts[] = {"invalid", "tolerance", "use_rqi", "vmax", nullptr};
148
149 /*---------------------------Execution Begins--------------------------------*/
150
151 /*
152 * Make sure there were command line options given. If not assign the
153 * name of the default ascii input file.
154 */
155 if (argc <= 1) {
156 print_usage();
157 exit(0);
158 }
159
160 /* Loop over each command line option */
161 while ((opt_let = getopt(argc, argv, "3264a:hm:l:nes:x:w:vyo:cg:fpSIC:")) != EOF) {
162
163 /* case over the option letter */
164 switch (opt_let) {
165 case 'v':
166 /* Should an output visualization file be output */
167 prob->vis_out = 1;
168 break;
169
170 case 'y':
171 /* Should an output visualization file be output */
172 prob->vis_out = 2;
173 break;
174
175 case 'x':
176 /* Undocumented flag for setting the error level */
177 if (optarg == nullptr) {
178 error_lev = 1;
179 }
180 else {
181 iret = sscanf(optarg, "%d", &error_lev);
182 if (iret != 1) {
183 error_lev = 1;
184 }
185
186 if (error_lev > 3) {
187 error_lev = 3;
188 }
189 }
190 break;
191
192 case 'c':
193 /* flag to allow the user to skip some error checks */
194 prob->skip_checks = 1;
195 break;
196
197 case 'C':
198 /* Specifiy (1-based) index of change set in mesh file */
199 if (optarg == nullptr) {
200 prob->selected_change_set = 0;
201 }
202 else {
203 iret = sscanf(optarg, "%d", &prob->selected_change_set);
204 if (iret != 1) {
205 prob->selected_change_set = 0;
206 }
207 }
208 break;
209
210 case 'f':
211 /*
212 * use the face method to calculate adjacencies for
213 * elemental decompositions
214 */
215 prob->face_adj = 1;
216 break;
217
218 case 'I':
219 /*
220 * detect vertical columns of elements and ensure that
221 * elements of a columns are all in one partition
222 */
223 prob->fix_columns = 1;
224 break;
225
226 case 'p':
227 /*
228 * use the partial method to determine adjacencies:
229 * only 3/4 of face nodes must match instead of all
230 */
231 prob->partial_adj = 1;
232 break;
233
234 case 'w':
235 /* Weighting options */
236 sub_opt = optarg;
237 while (sub_opt != nullptr && *sub_opt != '\0') {
238 switch (my_getsubopt(&sub_opt, (char *const *)weight_subopts, &value)) {
239 case READ_EXO:
240 if (value == nullptr || strlen(value) == 0) {
241 ctemp =
242 fmt::format("FATAL: must specify a file name with {}", weight_subopts[READ_EXO]);
243 Gen_Error(0, ctemp);
244 return 0;
245 }
246
247 if (weight->type < 0) {
248 weight->type = READ_EXO;
249 }
250 else if (!(weight->type & READ_EXO)) {
251 weight->type += READ_EXO;
252 }
253
254 /* check if the read is after an element block weight */
255 if (weight->type & EL_BLK) {
256 weight->ow_read = 0;
257 }
258
259 weight->exo_filename = value;
260
261 break; /* End "case READ_EXO" */
262
263 case VAR_INDX:
264 if (value == nullptr || strlen(value) == 0) {
265 ctemp = fmt::format("FATAL: must specify a value with {}", weight_subopts[VAR_INDX]);
266 Gen_Error(0, ctemp);
267 return 0;
268 }
269
270 iret = sscanf(value, "%d", &(weight->exo_vindx));
271 if (iret != 1) {
272 ctemp = fmt::format("FATAL: invalid value specified for {}", weight_subopts[VAR_INDX]);
273 Gen_Error(0, ctemp);
274 return 0;
275 }
276 break;
277
278 case TIME_INDX:
279 if (value == nullptr || strlen(value) == 0) {
280 ctemp = fmt::format("FATAL: must specify a value with {}", weight_subopts[TIME_INDX]);
281 Gen_Error(0, ctemp);
282 return 0;
283 }
284
285 iret = sscanf(value, "%d", &(weight->exo_tindx));
286 if (iret != 1) {
287 ctemp = fmt::format("FATAL: invalid value specified for {}", weight_subopts[TIME_INDX]);
288 Gen_Error(0, ctemp);
289 return 0;
290 }
291 break;
292
293 case VAR_NAME:
294 if (value == nullptr || strlen(value) == 0) {
295 ctemp = fmt::format("FATAL: must specify a value with {}", weight_subopts[VAR_NAME]);
296 Gen_Error(0, ctemp);
297 return 0;
298 }
299
300 weight->exo_varname = value;
301
302 break;
303
304 case EL_BLK:
305 if (value == nullptr) {
306 ctemp = fmt::format("FATAL: must specify an element block and weight with {}",
307 weight_subopts[EL_BLK]);
308 Gen_Error(0, ctemp);
309 return 0;
310 }
311 el_blk = -1;
312 wgt = -1;
313
314 iret = sscanf(value, "%d:%d", &el_blk, &wgt);
315 if (iret != 2) {
316 Gen_Error(0, "invalid element block weight");
317 return 0;
318 }
319 if (el_blk <= 0) {
320 ctemp = fmt::format("invalid element block, %d", el_blk);
321 Gen_Error(0, ctemp);
322 return 0;
323 }
324 if (wgt < 0) {
325 ctemp = fmt::format("invalid weight, %d", wgt);
326 Gen_Error(0, ctemp);
327 return 0;
328 }
329
330 weight->elemblk.push_back(el_blk);
331 weight->elemblk_wgt.push_back(wgt);
332
333 if (weight->type < 0) {
334 weight->type = EL_BLK;
335 }
336 else if (!(weight->type & EL_BLK)) {
337 weight->type += EL_BLK;
338 }
339
340 /* check if the element block weight needs to over write the read */
341 if (weight->type & READ_EXO) {
342 weight->ow_read = 1;
343 }
344
345 break;
346
347 case EDGE_WGT:
348 if (weight->type < 0) {
349 weight->type = EDGE_WGT;
350 }
351 else if (!(weight->type & EDGE_WGT)) {
352 weight->type += EDGE_WGT;
353 }
354 break;
355
356 case NO_WEIGHT: weight->type = NO_WEIGHT; break;
357
358 default:
359 ctemp = fmt::format("FATAL: unknown suboption {} specified", value);
360 Gen_Error(0, ctemp);
361 return 0;
362
363 } /* End "switch(my_getsubopt(&sub_opt, weight_subopts, &value))" */
364
365 } /* End "while(*sub_opt != '\0')" */
366 break; /* End "case 'w'" */
367
368 case 'a':
369 /* Only an ASCII input file name */
370 if (optarg != nullptr) {
371 ascii_inp_file = optarg;
372 }
373 break;
374
375 case 'o':
376 /* Output NemesisI file name */
377 if (optarg != nullptr) {
378 nemI_out_file = optarg;
379 }
380 break;
381
382 case 'n':
383 /* Nodal decomposition */
384 if (prob->type == DecompType::ELEMENTAL) {
385 Gen_Error(0, "FATAL: -e and -n are mutually exclusive");
386 return 0;
387 }
388 prob->type = DecompType::NODAL;
389 break;
390
391 case 'e':
392 /* Elemental decomposition */
393 if (prob->type == DecompType::NODAL) {
394 Gen_Error(0, "FATAL: -e and -n are mutually exclusive\n");
395 return 0;
396 }
397 prob->type = DecompType::ELEMENTAL;
398 break;
399
400 case 'm':
401 /* Machine type */
402 sub_opt = optarg;
403 if (sub_opt != nullptr) {
404 string_to_lower(sub_opt, '\0');
405 }
406 while (sub_opt != nullptr && *sub_opt != '\0') {
407
408 /* Switch over the machine description */
409 switch ((MachineType)my_getsubopt(&sub_opt, (char *const *)mach_subopts, &value)) {
410 case MachineType::HCUBE:
411 case MachineType::HYPERCUBE:
412 if (machine->type == MachineType::INVALID) {
413 machine->type = MachineType::HCUBE;
414 max_dim = 1;
415 }
416 FALL_THROUGH;
417
418 case MachineType::MESH:
419 if (machine->type == MachineType::INVALID) {
420 machine->type = MachineType::MESH;
421 max_dim = 3;
422 }
423
424 cptr = value; /* want to set this for both mesh and hcube */
425 FALL_THROUGH;
426
427 case MachineType::CLUSTER:
428 if (machine->type == MachineType::INVALID) /* so, get the number of boxes */
429 {
430 if (value == nullptr || strlen(value) == 0) {
431 Gen_Error(0, "FATAL: need to specify number of boxes");
432 return 0;
433 }
434
435 /* now need to find what each box consists of */
436 cptr = strpbrk(value, "mMhH");
437 if (*cptr == 'm' || *cptr == 'M') {
438 machine->type = MachineType::MESH;
439 max_dim = 3;
440 }
441 else if (*cptr == 'h' || *cptr == 'H') {
442 machine->type = MachineType::HCUBE;
443 max_dim = 1;
444 }
445 else {
446 Gen_Error(0, "FATAL: unknown type specified with cluster");
447 return 0;
448 }
449 /* blank out character and move cptr to next char */
450 *cptr = '\0';
451 cptr++;
452
453 /* get the number of boxes from value */
454 iret = sscanf(value, "%d", &(machine->num_boxes));
455 if (iret <= 0 || machine->num_boxes <= 0) {
456 Gen_Error(0, "FATAL: invalid number of boxes");
457 return 0;
458 }
459 }
460
461 if (cptr == nullptr || strlen(cptr) == 0) {
462 Gen_Error(0, "FATAL: need to specify dimension");
463 return 0;
464 }
465 cptr2 = strtok(cptr, "xX");
466 if (cptr2 == nullptr) {
467 Gen_Error(0, "FATAL: bad size for dimension specification");
468 return 0;
469 }
470 machine->num_dims = 0;
471 for (i = 0; i < max_dim; i++) {
472 machine->dim[i] = 1;
473 }
474 while (cptr2) {
475 iret = sscanf(cptr2, "%d", &(machine->dim[machine->num_dims]));
476 if (iret <= 0 || machine->dim[machine->num_dims] <= 0) {
477 Gen_Error(0, "FATAL: invalid dimension specification");
478 return 0;
479 }
480
481 machine->num_dims++;
482 cptr2 = strtok(nullptr, "xX");
483
484 /* Only up to three-dimensional allowed */
485 if (machine->num_dims == max_dim && cptr2 != nullptr) {
486 Gen_Error(0, "FATAL: maximum number of dimensions exceeded");
487 return 0;
488 }
489 }
490
491 break; /* End "case MESH or HCUBE or CLUSTER" */
492
493 default: Gen_Error(0, "FATAL: unknown machine type"); return 0;
494
495 } /* End "switch(my_getsubopt(&sub_opt, mach_subopts, &value))" */
496
497 } /* End "while(*sub_opt != '\0')" */
498 break; /* End "case 'm'" */
499
500 case 'l':
501 /* Load balance information */
502 sub_opt = optarg;
503 if (sub_opt != nullptr) {
504 string_to_lower(sub_opt, '\0');
505 }
506 while (sub_opt != nullptr && *sub_opt != '\0') {
507 switch ((Balance)my_getsubopt(&sub_opt, (char *const *)lb_subopts, &value)) {
508 case Balance::MULTIKL: lb->type = Balance::MULTIKL; break;
509
510 case Balance::SPECTRAL: lb->type = Balance::SPECTRAL; break;
511
512 case Balance::INERTIAL: lb->type = Balance::INERTIAL; break;
513
514 case Balance::ZPINCH: lb->type = Balance::ZPINCH; break;
515
516 case Balance::BRICK: lb->type = Balance::BRICK; break;
517
518 case Balance::ZOLTAN_RCB: lb->type = Balance::ZOLTAN_RCB; break;
519
520 case Balance::ZOLTAN_RIB: lb->type = Balance::ZOLTAN_RIB; break;
521
522 case Balance::ZOLTAN_HSFC: lb->type = Balance::ZOLTAN_HSFC; break;
523
524 case Balance::LINEAR: lb->type = Balance::LINEAR; break;
525
526 case Balance::RANDOM: lb->type = Balance::RANDOM; break;
527
528 case Balance::SCATTERED: lb->type = Balance::SCATTERED; break;
529
530 case Balance::INFILE:
531 if (value == nullptr) {
532 Gen_Error(0, "FATAL: need to specify a value with file");
533 return 0;
534 }
535 char tmpstr[2048];
536 iret = sscanf(value, "%2047s", tmpstr);
537 lb->file = tmpstr;
538 if (iret != 1) {
539 Gen_Error(0, "FATAL: invalid value associated with file");
540 return 0;
541 }
542 lb->type = Balance::INFILE;
543 break;
544
545 case Balance::KL_REFINE: lb->refine = Balance::KL_REFINE; break;
546
547 case Balance::NO_REFINE: lb->refine = Balance::NO_REFINE; break;
548
549 case Balance::NUM_SECTS:
550 if (value == nullptr) {
551 Gen_Error(0, "FATAL: need to specify a value with num_sects");
552 return 0;
553 }
554 iret = sscanf(value, "%d", &(lb->num_sects));
555 if (iret != 1) {
556 Gen_Error(0, "FATAL: invalid value associated with num_sects");
557 return 0;
558 }
559 break;
560
561 case Balance::CNCT_DOM: lb->cnctd_dom = 1; break;
562
563 case Balance::IGNORE_Z: lb->ignore_z = 1; break;
564
565 case Balance::OUTFILE:
566 if (value == nullptr) {
567 Gen_Error(0, "FATAL: need to specify a value with outfile");
568 return 0;
569 }
570 iret = sscanf(value, "%2047s", tmpstr);
571 lb->file = tmpstr;
572 if (iret != 1) {
573 Gen_Error(0, "FATAL: invalid value associated with outfile");
574 return 0;
575 }
576 lb->outfile = ELB_TRUE;
577 break;
578
579 default:
580 ctemp = fmt::format("FATAL: unknown lb param \"{}\"", value);
581 Gen_Error(0, ctemp);
582 return 0;
583
584 } /* End "switch(my_getsubopt(&sup_opt, mach_subopts, &value))" */
585
586 } /* End "while(*sup_opt != '\0')" */
587 break; /* End "case 'l'" */
588
589 case 'S': prob->no_sph = 1; break;
590
591 case 's':
592 /* Eigen solver options */
593 sub_opt = optarg;
594 if (sub_opt != nullptr) {
595 string_to_lower(sub_opt, '\0');
596 }
597 while (sub_opt != nullptr && *sub_opt != '\0') {
598 switch ((SolverOptions)my_getsubopt(&sub_opt, (char *const *)solve_subopts, &value)) {
599 case SolverOptions::TOLER:
600 if (value == nullptr) {
601 fmt::print(stderr, "FATAL: tolerance specification requires \
602 value\n");
603 return 0;
604 }
605 iret = sscanf(value, "%le", &(solver->tolerance));
606 if (iret != 1) {
607 fmt::print(stderr, "FATAL: incorrect value for tolerance\n");
608 return 0;
609 }
610
611 break;
612
613 case SolverOptions::USE_RQI:
614 if (solver->rqi_flag == SolverOptions::USE_RQI) {
615 solver->rqi_flag = SolverOptions::INVALID;
616 }
617 else {
618 solver->rqi_flag = SolverOptions::USE_RQI;
619 }
620
621 break;
622
623 case SolverOptions::VMAX:
624 if (value == nullptr) {
625 fmt::print(stderr, "FATAL: must specify a value with {}\n",
626 solve_subopts[(int)SolverOptions::VMAX]);
627 return 0;
628 }
629 iret = sscanf(value, "%d", &(solver->vmax));
630 if (iret != 1) {
631 fmt::print(stderr, "FATAL: invalid value read for {}\n",
632 solve_subopts[(int)SolverOptions::VMAX]);
633 return 0;
634 }
635
636 break;
637
638 default: fmt::print(stderr, "FATAL: unknown solver option\n"); return 0;
639
640 } /* End "switch(my_getsubopt(&sub_opt, solve_subopts, &value))" */
641
642 } /* End "while(sub_opt != '\0')" */
643 break; /* End "case 's'" */
644
645 case 'g':
646 /* group designations */
647 /* allocate string to hold designation */
648 if (optarg != nullptr) {
649 prob->groups = reinterpret_cast<char *>(malloc(strlen(optarg) + 1));
650 copy_string(prob->groups, optarg, strlen(optarg) + 1);
651 }
652 break;
653
654 case 'h':
655 /* Usage info was requested */
656 print_usage();
657 exit(0);
658
659 case '6':
660 case '4':
661 /* ignore -- used to parse -64 option */
662 break;
663
664 case '3':
665 case '2':
666 /* ignore -- used to parse -32 option */
667 break;
668
669 default:
670 /* Default case. Error on unknown argument. */
671 return 0;
672
673 } /* End "switch(opt_let)" */
674
675 } /* End "while((opt_let=getopt(argc, argv, "i")) != EOF)" */
676 /* Get the input file name, if specified on the command line */
677 if ((argc - optind) >= 1) {
678 exoII_inp_file = argv[optind];
679 }
680 else {
681 exoII_inp_file = "";
682 }
683 return 1;
684
685 } /*-------End cmd_line_arg_parse()--------*/
686
687 /*****************************************************************************/
688 /*****************************************************************************/
689 /*****************************************************************************/
690 /* This function reads in the ASCII command file.
691 *---------------------------------------------------------------------------*/
692 template int read_cmd_file(std::string &ascii_inp_file, std::string &exoII_inp_file,
693 std::string &nemI_out_file, Machine_Description *machine,
694 LB_Description<int> *lb, Problem_Description *problem,
695 Solver_Description *solver, Weight_Description *weight);
696 template int read_cmd_file(std::string &ascii_inp_file, std::string &exoII_inp_file,
697 std::string &nemI_out_file, Machine_Description *machine,
698 LB_Description<int64_t> *lb, Problem_Description *problem,
699 Solver_Description *solver, Weight_Description *weight);
700
701 template <typename INT>
702 int read_cmd_file(std::string &ascii_inp_file, std::string &exoII_inp_file,
703 std::string &nemI_out_file, Machine_Description *machine, LB_Description<INT> *lb,
704 Problem_Description *problem, Solver_Description *solver,
705 Weight_Description *weight)
706 {
707 FILE *inp_fd;
708 std::string ctemp;
709 char inp_line[MAX_INP_LINE];
710 char inp_copy[MAX_INP_LINE];
711 char *cptr, *cptr2;
712
713 int iret;
714 int el_blk;
715 int wgt;
716 int i;
717 int ilen;
718 int max_dim;
719 char tmpstr[2048];
720 /*-----------------------------Execution Begins------------------------------*/
721 if (!(inp_fd = fopen(ascii_inp_file.c_str(), "r"))) {
722 ctemp = fmt::format("FATAL: unable to open ASCII input file {}", ascii_inp_file);
723 Gen_Error(0, ctemp);
724 return 0;
725 }
726 ON_BLOCK_EXIT(fclose, inp_fd);
727 /* Begin parsing the input file */
728 while (fgets(inp_line, MAX_INP_LINE, inp_fd)) {
729 if (inp_line[0] != '#') {
730 copy_string(inp_copy, inp_line);
731 clean_string(inp_line, " \t");
732 cptr = strtok(inp_line, "\t=");
733 if (token_compare(cptr, "input exodusii file")) {
734 /* The input ExodusII file name */
735 if (exoII_inp_file.empty()) {
736 cptr = strtok(nullptr, "\t=");
737 strip_string(cptr, " \t\n");
738 exoII_inp_file = cptr;
739 }
740 }
741 else if (token_compare(cptr, "output visualization file")) {
742 if (problem->vis_out < 0) {
743 /* Output a visualization file */
744 cptr = strtok(nullptr, "\t=");
745 strip_string(cptr, " \t\n");
746 if (strcasecmp(cptr, "yes") == 0 || strcasecmp(cptr, "true") == 0) {
747 problem->vis_out = 1;
748 }
749 else if (strcasecmp(cptr, "no") == 0 || strcasecmp(cptr, "false") == 0) {
750 problem->vis_out = 0;
751 }
752 else {
753 iret = sscanf(cptr, "%d", &problem->vis_out);
754 if (iret != 1) {
755 Gen_Error(1, "WARNING: unknown visualization output flag");
756 problem->vis_out = 0;
757 }
758 else {
759 if (problem->vis_out != 1 && problem->vis_out != 2) {
760 problem->vis_out = 0;
761 }
762 }
763 }
764 }
765 }
766 else if (token_compare(cptr, "output nemesisi file")) {
767 /* The NemesisI output file name */
768 if (nemI_out_file.empty()) {
769 cptr = strtok(nullptr, "\t=");
770 strip_string(cptr, " \t\n");
771 nemI_out_file = cptr;
772 }
773 }
774 else if (token_compare(cptr, "decomposition method")) {
775 /* The method to use for decomposing the graph */
776 if (lb->type == Balance::INVALID || lb->refine == Balance::INVALID || lb->num_sects < 0) {
777
778 /* Search to the first null character */
779 cptr = strchr(cptr, '\0');
780 cptr++;
781 strip_string(cptr, " \t\n=");
782 cptr = strtok(cptr, ",");
783 while (cptr != nullptr) {
784 strip_string(cptr, " \t\n");
785 string_to_lower(cptr, '\0');
786 if (strcmp(cptr, "multikl") == 0) {
787 if (lb->type == Balance::INVALID) {
788 lb->type = Balance::MULTIKL;
789 }
790 }
791 else if (strcmp(cptr, "spectral") == 0) {
792 if (lb->type == Balance::INVALID) {
793 lb->type = Balance::SPECTRAL;
794 }
795 }
796 else if (strcmp(cptr, "scattered") == 0) {
797 if (lb->type == Balance::INVALID) {
798 lb->type = Balance::SCATTERED;
799 }
800 }
801 else if (strcmp(cptr, "linear") == 0) {
802 if (lb->type == Balance::INVALID) {
803 lb->type = Balance::LINEAR;
804 }
805 }
806 else if (strcmp(cptr, "inertial") == 0) {
807 if (lb->type == Balance::INVALID) {
808 lb->type = Balance::INERTIAL;
809 }
810 }
811 else if (strcmp(cptr, "zpinch") == 0) {
812 if (lb->type == Balance::INVALID) {
813 lb->type = Balance::ZPINCH;
814 }
815 }
816 else if (strcmp(cptr, "brick") == 0) {
817 if (lb->type == Balance::INVALID) {
818 lb->type = Balance::BRICK;
819 }
820 }
821 else if (strcmp(cptr, "rcb") == 0) {
822 if (lb->type == Balance::INVALID) {
823 lb->type = Balance::ZOLTAN_RCB;
824 }
825 }
826 else if (strcmp(cptr, "rib") == 0) {
827 if (lb->type == Balance::INVALID) {
828 lb->type = Balance::ZOLTAN_RIB;
829 }
830 }
831 else if (strcmp(cptr, "hsfc") == 0) {
832 if (lb->type == Balance::INVALID) {
833 lb->type = Balance::ZOLTAN_HSFC;
834 }
835 }
836 else if (strcmp(cptr, "random") == 0) {
837 if (lb->type == Balance::INVALID) {
838 lb->type = Balance::RANDOM;
839 }
840 }
841 else if (strstr(cptr, "infile")) {
842 if (lb->type == Balance::INVALID) {
843 lb->type = Balance::INFILE;
844 cptr2 = strchr(cptr, '=');
845 if (cptr2 == nullptr) {
846 Gen_Error(0, "FATAL: need to specify a value with infile");
847 return 0;
848 }
849
850 cptr2++;
851 iret = sscanf(cptr2, "%2047s", tmpstr);
852 lb->file = tmpstr;
853 if (iret != 1) {
854 Gen_Error(0, "FATAL: invalid value for infile");
855 return 0;
856 }
857 }
858 }
859 else if (strcmp(cptr, "kl") == 0) {
860 if (lb->refine == Balance::INVALID) {
861 lb->refine = Balance::KL_REFINE;
862 }
863 }
864 else if (strcmp(cptr, "none") == 0) {
865 ; // Do nothing.
866 }
867 else if (strcmp(cptr, "ignore_z") == 0) {
868 lb->ignore_z = 1;
869 }
870 else if (strstr(cptr, "num_sects")) {
871 if (lb->num_sects < 0) {
872 cptr2 = strchr(cptr, '=');
873 if (cptr2 == nullptr) {
874 Gen_Error(0, "FATAL: need to specify a value with num_sects");
875 return 0;
876 }
877
878 cptr2++;
879 iret = sscanf(cptr2, "%d", &(lb->num_sects));
880 if (iret != 1) {
881 Gen_Error(0, "FATAL: invalid value for num_sects");
882 return 0;
883 }
884 }
885 }
886 else if (strcmp(cptr, "cnctd_dom") == 0) {
887 if (lb->cnctd_dom < 0) {
888 lb->cnctd_dom = ELB_TRUE;
889 }
890 }
891 else if (strstr(cptr, "outfile")) {
892 if (lb->outfile < 0) {
893 lb->outfile = ELB_TRUE;
894 cptr2 = strchr(cptr, '=');
895 if (cptr2 == nullptr) {
896 Gen_Error(0, "FATAL: need to specify a value with outfile");
897 return 0;
898 }
899
900 cptr2++;
901 iret = sscanf(cptr2, "%2047s", tmpstr);
902 lb->file = tmpstr;
903 if (iret != 1) {
904 Gen_Error(0, "FATAL: invalid value for outfile");
905 return 0;
906 }
907 }
908 }
909 else {
910 ctemp =
911 fmt::format("FATAL: unknown LB method \"{}\" specified in command file", cptr);
912 Gen_Error(0, ctemp);
913 return 0;
914 }
915 cptr = strtok(nullptr, ",");
916 }
917 }
918 }
919 else if (token_compare(cptr, "solver specifications")) {
920 /* Solver specs */
921
922 /* Search to the first null character */
923 cptr = strchr(cptr, '\0');
924 cptr++;
925 strip_string(cptr, " \t\n=");
926 cptr = strtok(cptr, ",");
927
928 /* Loop until all the suboptions have been specified */
929 while (cptr) {
930 strip_string(cptr, " \t\n");
931 string_to_lower(cptr, '\0');
932
933 /* Check to see if this is the "tolerance" suboption */
934 if (strstr(cptr, "tolerance")) {
935 if (solver->tolerance < 0.0) {
936 cptr2 = strchr(cptr, '=');
937 if (cptr2 == nullptr) {
938 Gen_Error(0, "FATAL: tolerance specification requires a value");
939 return 0;
940 }
941
942 cptr2++;
943 iret = sscanf(cptr2, "%le", &(solver->tolerance));
944 if (iret != 1) {
945 Gen_Error(0, "FATAL: invalid value for tolerance");
946 return 0;
947 }
948 }
949 }
950 else if (strcmp(cptr, "use_rqi") == 0) {
951 if (solver->rqi_flag == SolverOptions::USE_RQI) {
952 solver->rqi_flag = SolverOptions::INVALID;
953 }
954 else {
955 solver->rqi_flag = SolverOptions::USE_RQI;
956 }
957 }
958 else if (strstr(cptr, "vmax")) {
959 if (solver->vmax < 0) {
960 cptr2 = strchr(cptr, '=');
961 if (cptr2 == nullptr) {
962 Gen_Error(0, "FATAL: vmax must have a value");
963 return 0;
964 }
965
966 cptr2++;
967 iret = sscanf(cptr2, "%d", &(solver->vmax));
968 if (iret != 1) {
969 Gen_Error(0, "FATAL: invalid value for vmax");
970 return 0;
971 }
972 }
973 }
974 else {
975 ctemp = fmt::format("WARNING: unknown solver suboption {}", cptr);
976 Gen_Error(1, ctemp);
977 }
978
979 cptr = strtok(nullptr, ",");
980 }
981 }
982 else if (token_compare(cptr, "graph type")) {
983 cptr = strtok(nullptr, "\t=");
984 strip_string(cptr, " \t\n");
985 string_to_lower(cptr, '\0');
986 if (strcmp(cptr, "nodal") == 0) {
987 problem->type = DecompType::NODAL;
988 }
989 else if (strcmp(cptr, "node") == 0) {
990 problem->type = DecompType::NODAL;
991 }
992 else if (strcmp(cptr, "elemental") == 0) {
993 problem->type = DecompType::ELEMENTAL;
994 }
995 else if (strcmp(cptr, "element") == 0) {
996 problem->type = DecompType::ELEMENTAL;
997 }
998 else {
999 Gen_Error(0, "FATAL: unknown graph type specified");
1000 return 0;
1001 }
1002 }
1003 else if (token_compare(cptr, "machine description")) {
1004 /* Machine specs */
1005 if (machine->num_dims < 0) {
1006 /* Search to first null character */
1007 cptr = strchr(cptr, '\0');
1008 cptr++;
1009 strip_string(cptr, " \t\n=");
1010
1011 /* Search to equal sign */
1012 cptr2 = strchr(cptr, '=');
1013 if (cptr2 == nullptr) {
1014 Gen_Error(0, "FATAL: machine must have a dimension specified");
1015 return 0;
1016 }
1017
1018 *cptr2 = '\0';
1019
1020 /* Find out the machine type */
1021 strip_string(cptr, " \t\n");
1022 if (strcasecmp(cptr, "mesh") == 0) {
1023 machine->type = MachineType::MESH;
1024 max_dim = 3;
1025 }
1026 else if (strcasecmp(cptr, "hcube") == 0 || strcasecmp(cptr, "hypercube") == 0) {
1027 machine->type = MachineType::HCUBE;
1028 max_dim = 1;
1029 }
1030 else if (strcasecmp(cptr, "cluster") == 0) {
1031 /* now need to find what each box consists of */
1032 cptr = cptr2 + 1;
1033 cptr2 = strpbrk(cptr, "mMhH");
1034 if (*cptr2 == 'm' || *cptr2 == 'M') {
1035 machine->type = MachineType::MESH;
1036 max_dim = 3;
1037 }
1038 else if (*cptr2 == 'h' || *cptr2 == 'H') {
1039 machine->type = MachineType::HCUBE;
1040 max_dim = 1;
1041 }
1042 else {
1043 Gen_Error(0, "FATAL: unknown type specified with cluster");
1044 return 0;
1045 }
1046 /* blank out character and move cptr to next char */
1047 *cptr2 = '\0';
1048
1049 /* get the number of boxes from value */
1050 iret = sscanf(cptr, "%d", &(machine->num_boxes));
1051 if (iret <= 0 || machine->num_boxes <= 0) {
1052 Gen_Error(0, "FATAL: invalid number of boxes");
1053 return 0;
1054 }
1055 }
1056 else {
1057 Gen_Error(0, "FATAL: unknown machine type specified");
1058 return 0;
1059 }
1060
1061 machine->num_dims = 0;
1062 cptr = cptr2 + 1;
1063 cptr = strtok(cptr, "xX");
1064 while (cptr) {
1065 iret = sscanf(cptr, "%d", &(machine->dim[machine->num_dims]));
1066 if (iret != 1) {
1067 Gen_Error(0, "FATAL: invalid dimension specified for machine");
1068 return 0;
1069 }
1070
1071 machine->num_dims++;
1072 cptr = strtok(nullptr, "xX");
1073
1074 /* Check how many dimensions there are */
1075 if (machine->num_dims == max_dim && cptr != nullptr) {
1076 Gen_Error(0, "FATAL: maximum number of dimensions exceeded");
1077 return 0;
1078 }
1079 }
1080 }
1081 }
1082 else if (token_compare(cptr, "weighting specifications")) {
1083 /* Parameters for weighting the graph */
1084 if (weight->type < 0) {
1085 cptr = strchr(cptr, '\0');
1086 cptr++;
1087 strip_string(cptr, " \t\n=");
1088 cptr = strtok(cptr, ",");
1089
1090 while (cptr != nullptr) {
1091 strip_string(cptr, " \t\n");
1092 string_to_lower(cptr, '\0');
1093 if (strstr(cptr, "read")) {
1094 cptr2 = strchr(cptr, '=');
1095 if (cptr2 == nullptr) {
1096 Gen_Error(0, "FATAL: must specify file name with \"read\"");
1097 return 0;
1098 }
1099 cptr2++;
1100 if (strlen(cptr2) == 0) {
1101 Gen_Error(0, "FATAL: invalid file name with \"read\"");
1102 return 0;
1103 }
1104 weight->exo_filename = cptr2;
1105 if (weight->type < 0) {
1106 weight->type = READ_EXO;
1107 }
1108 else if (!(weight->type & READ_EXO)) {
1109 weight->type += READ_EXO;
1110 }
1111
1112 /* check if the read is after an element block weight */
1113 if (weight->type & EL_BLK) {
1114 weight->ow_read = 0;
1115 }
1116 }
1117 else if (strstr(cptr, "var_name")) {
1118 cptr2 = strchr(cptr, '=');
1119 if (cptr2 == nullptr) {
1120 Gen_Error(0, "FATAL: must specify a name with \"var_name\"");
1121 return 0;
1122 }
1123 cptr2++;
1124 if (strlen(cptr2) == 0) {
1125 Gen_Error(0, "FATAL: invalid variable name specified with"
1126 " \"var_name\"");
1127 return 0;
1128 }
1129 weight->exo_varname = cptr2;
1130 }
1131 else if (strstr(cptr, "var_index")) {
1132 cptr2 = strchr(cptr, '=');
1133 if (cptr2 == nullptr) {
1134 Gen_Error(0, "FATAL: must specify a value with \"var_index\"");
1135 return 0;
1136 }
1137 cptr2++;
1138
1139 iret = sscanf(cptr2, "%d", &(weight->exo_vindx));
1140 if (iret != 1) {
1141 Gen_Error(0, "FATAL: invalid value with \"var_index\"");
1142 return 0;
1143 }
1144 }
1145 else if (strstr(cptr, "time_index")) {
1146 cptr2 = strchr(cptr, '=');
1147 if (cptr2 == nullptr) {
1148 Gen_Error(0, "FATAL: must specify a value with \"time_index\"");
1149 return 0;
1150 }
1151 cptr2++;
1152
1153 iret = sscanf(cptr2, "%d", &(weight->exo_tindx));
1154 if (iret != 1) {
1155 Gen_Error(0, "FATAL: invalid value with \"time_index\"");
1156 return 0;
1157 }
1158 }
1159 else if (strstr(cptr, "eb")) {
1160 cptr2 = strchr(cptr, '=');
1161 if (cptr2 == nullptr) {
1162 Gen_Error(0, "FATAL: must specify a value with \"eb\"");
1163 return 0;
1164 }
1165 cptr2++;
1166
1167 el_blk = -1;
1168 wgt = -1;
1169
1170 iret = sscanf(cptr2, "%d:%d", &el_blk, &wgt);
1171 if (iret != 2) {
1172 Gen_Error(0, "FATAL: invalid value with \"eb\"");
1173 return 0;
1174 }
1175 if (el_blk <= 0) {
1176 ctemp = fmt::format("invalid element block, %d", el_blk);
1177 Gen_Error(0, ctemp);
1178 return 0;
1179 }
1180 if (wgt < 1) {
1181 ctemp = fmt::format("invalid weight, %d", wgt);
1182 Gen_Error(0, ctemp);
1183 return 0;
1184 }
1185
1186 if (weight->type < 0) {
1187 weight->type = EL_BLK;
1188 }
1189 else if (!(weight->type & EL_BLK)) {
1190 weight->type += EL_BLK;
1191 }
1192
1193 weight->elemblk.push_back(el_blk);
1194 weight->elemblk_wgt.push_back(wgt);
1195
1196 /* check if the elem block weight needs to overwrite the read */
1197 if (weight->type & READ_EXO) {
1198 weight->ow_read = 1;
1199 }
1200 }
1201 else if (strstr(cptr, "edges")) {
1202 if (weight->type < 0) {
1203 weight->type = EDGE_WGT;
1204 }
1205 else if (!(weight->type & EDGE_WGT)) {
1206 weight->type += EDGE_WGT;
1207 }
1208 }
1209 else {
1210 ctemp = fmt::format("FATAL: unknown suboption \"{}\" specified", cptr);
1211 Gen_Error(0, ctemp);
1212 return 0;
1213 }
1214
1215 cptr = strtok(nullptr, ",");
1216 }
1217
1218 } /* End "if(weight->type < 0)" */
1219 }
1220 else if (token_compare(cptr, "misc options")) {
1221 /* Misc Options */
1222
1223 /* Search to the first null character */
1224 cptr = strchr(cptr, '\0');
1225 cptr++;
1226 /*
1227 * Need to handle case where users have put comma's in
1228 * the group descriptor. This will mess up getting the
1229 * tokens using strtok(). So, search for commas between
1230 * the beginning delimiter, "{", and the end delimiter,
1231 * "}", and change them to blank spaces.
1232 */
1233 cptr2 = strchr(cptr, '{');
1234 if (cptr2 != nullptr) {
1235 ilen = strlen(cptr2);
1236 for (i = 0; i < ilen; i++) {
1237 if (*cptr2 == '}') {
1238 break;
1239 }
1240 if (*cptr2 == ',') {
1241 *cptr2 = ' ';
1242 }
1243 cptr2++;
1244 }
1245 }
1246
1247 strip_string(cptr, " \t\n=");
1248 cptr = strtok(cptr, ",");
1249
1250 /* Loop until all the suboptions have been specified */
1251 while (cptr != nullptr) {
1252 strip_string(cptr, " \t\n");
1253 string_to_lower(cptr, '\0');
1254
1255 /* Check to see if the side id error checks need to be skipped */
1256 if (strstr(cptr, "checks_off")) {
1257 if (problem->skip_checks < 0) {
1258 problem->skip_checks = 1;
1259 }
1260 }
1261 /* Check to see if using face definition of adjacency */
1262 else if (strstr(cptr, "face_adj")) {
1263 if (problem->face_adj < 0) {
1264 problem->face_adj = 1;
1265 }
1266 }
1267 /* Check if element columns are to be detected and fixed so
1268 * that all elements of a column are in the same partition */
1269 else if (strstr(cptr, "fix_columns")) {
1270 problem->fix_columns = 1;
1271 }
1272 /* Check to see if looking for global mechanisms */
1273 else if (strstr(cptr, "global_mech")) {
1274 if (problem->global_mech < 0) {
1275 problem->global_mech = 1;
1276 }
1277 }
1278 /* Check to see if looking for introduced mechanisms */
1279 else if (strstr(cptr, "local_mech")) {
1280 if (problem->local_mech < 0) {
1281 problem->local_mech = 1;
1282 }
1283 }
1284 /* Check to see if looking for connected domains */
1285 else if (strstr(cptr, "find_cnt_domains")) {
1286 if (problem->find_cnt_domains < 0) {
1287 problem->find_cnt_domains = 1;
1288 }
1289 }
1290 /* Check to see if user wants to add processors to take care of
1291 * introduced mechanisms
1292 */
1293 else if (strstr(cptr, "mech_add_procs")) {
1294 if (problem->mech_add_procs < 0) {
1295 problem->mech_add_procs = 1;
1296 }
1297 }
1298 /* Check to see if user wants to add processors to take care of
1299 * introduced mechanisms
1300 */
1301 else if (strstr(cptr, "dsd_add_procs")) {
1302 if (problem->dsd_add_procs < 0) {
1303 problem->dsd_add_procs = 1;
1304 }
1305 }
1306 /* Check for treating spheres as concentrated masses*/
1307 else if (strstr(cptr, "no_sph")) {
1308 if (problem->no_sph < 0) {
1309 problem->no_sph = 1;
1310 }
1311 }
1312 /* Check for group designation sub-option */
1313 else if (strstr(cptr, "groups")) {
1314 /* "{" defines the beginning of the group designator */
1315 cptr2 = strchr(cptr, '{');
1316 if (cptr2 == nullptr) {
1317 Gen_Error(0, "FATAL: group start designator \"}\" not found");
1318 return 0;
1319 }
1320 cptr2++;
1321 /* allocate space to hold the group designator */
1322 problem->groups = reinterpret_cast<char *>(malloc(strlen(cptr2) + 1));
1323 copy_string(problem->groups, cptr2, strlen(cptr2) + 1);
1324 /* get rid of ending bracket */
1325 cptr2 = strchr(problem->groups, '}');
1326 *cptr2 = '\0';
1327 }
1328 else {
1329 ctemp = fmt::format("WARNING: unknown miscellaneous suboption {}", cptr);
1330 Gen_Error(1, ctemp);
1331 }
1332 cptr = strtok(nullptr, ",");
1333 }
1334 }
1335 else {
1336 /* Generate an error, but continue reading for an unknown key */
1337 strip_string(inp_copy, " #\t");
1338 if (strlen(inp_copy) > 5) {
1339 ctemp = fmt::format(
1340 "WARNING: don't know how to process line: \n{}\nin command file, ignored", inp_copy);
1341 Gen_Error(1, ctemp);
1342 }
1343 }
1344
1345 } /* End "if(inp_line[0] != '#')" */
1346
1347 } /* End "while(fgets(inp_line, MAX_INP_LINE, inp_fd))" */
1348 return 1;
1349 } /*------------End read_cmd_file()---------------*/
1350
1351 /*****************************************************************************/
1352 /*****************************************************************************/
1353 /*****************************************************************************/
1354 /* This function performs error checks on the user input.
1355 *---------------------------------------------------------------------------*/
1356 template int check_inp_specs(std::string &exoII_inp_file, std::string &nemI_out_file,
1357 Machine_Description *machine, LB_Description<int> *lb,
1358 Problem_Description *problem, Solver_Description *solver,
1359 Weight_Description *weight);
1360
1361 template int check_inp_specs(std::string &exoII_inp_file, std::string &nemI_out_file,
1362 Machine_Description *machine, LB_Description<int64_t> *lb,
1363 Problem_Description *problem, Solver_Description *solver,
1364 Weight_Description *weight);
1365
1366 template <typename INT>
1367 int check_inp_specs(std::string &exoII_inp_file, std::string &nemI_out_file,
1368 Machine_Description *machine, LB_Description<INT> *lb,
1369 Problem_Description *problem, Solver_Description *solver,
1370 Weight_Description *weight)
1371 {
1372 /* Check that an input ExodusII file name was specified */
1373 if (exoII_inp_file.empty()) {
1374 Gen_Error(0, "FATAL: no input ExodusII file specified");
1375 return 0;
1376 }
1377
1378 /* Check for the existence and readability of the input file */
1379 int icpu_ws = 0;
1380 int iio_ws = 0;
1381 float vers;
1382 int exid_inp;
1383 if ((exid_inp = ex_open(exoII_inp_file.c_str(), EX_READ, &icpu_ws, &iio_ws, &vers)) < 0) {
1384 std::string ctemp = fmt::format("FATAL: unable to open input ExodusII file {}", exoII_inp_file);
1385 Gen_Error(0, ctemp);
1386 return 0;
1387 }
1388
1389 // Determine whether there are any change sets in file.
1390 // If there are, check whether user specified a specific
1391 // change set index and set to that one (if valid), or
1392 // if not specified, set to the first.
1393 exid_inp = check_change_sets(exid_inp, problem->selected_change_set);
1394 if (exid_inp == 0) {
1395 return 0;
1396 }
1397
1398 /* Get the integer size stored on the database */
1399 problem->int64db = ex_int64_status(exid_inp) & EX_ALL_INT64_DB;
1400
1401 ex_close(exid_inp);
1402 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1403 /* Check the machine specification */
1404 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1405 if (machine->type != MachineType::MESH && machine->type != MachineType::HCUBE) {
1406 Gen_Error(0, "FATAL: machine type not properly set");
1407 return 0;
1408 }
1409 if (machine->type == MachineType::HCUBE && machine->num_dims != 1) {
1410 Gen_Error(0, "FATAL: improper number of dimension for a hypercube, only"
1411 " 1 allowed");
1412 return 0;
1413 }
1414 if (machine->type == MachineType::MESH && machine->num_dims > 3) {
1415 Gen_Error(0, "FATAL: maximum of 3 dimensions for a mesh exceeded");
1416 return 0;
1417 }
1418
1419 /* non-cluster machines have only one box */
1420 if (machine->num_boxes < 0) {
1421 machine->num_boxes = 1;
1422 }
1423
1424 /* Find out the number of processors */
1425 if (machine->type == MachineType::HCUBE) {
1426 machine->procs_per_box = 1 << machine->dim[0];
1427 }
1428 else {
1429 machine->procs_per_box = machine->dim[0];
1430 for (int cnt = 1; cnt < machine->num_dims; cnt++) {
1431 machine->procs_per_box *= machine->dim[cnt];
1432 }
1433 }
1434
1435 /* now calculate the total number of processors */
1436 machine->num_procs = machine->num_boxes * machine->procs_per_box;
1437
1438 /*
1439 * currently, do not allow groups and clusters since the
1440 * loops to chaco get a bit too confusing
1441 */
1442 if (machine->num_boxes > 1 && problem->groups != nullptr) {
1443 Gen_Error(0, "FATAL: groups cannot be designated for a cluster machine");
1444 return 0;
1445 }
1446
1447 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1448 /* Check the problem specifications */
1449 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1450 if (problem->type != DecompType::ELEMENTAL && problem->type != DecompType::NODAL) {
1451 Gen_Error(0, "FATAL: unknown problem type specified");
1452 return 0;
1453 }
1454
1455 if (problem->skip_checks < 0) {
1456 problem->skip_checks = 0;
1457 }
1458
1459 if (problem->face_adj < 0) {
1460 problem->face_adj = 0;
1461 }
1462
1463 /*
1464 * using face definition of adjacencies only makes sense
1465 * with an elemental decomposition
1466 */
1467 if (problem->type != DecompType::ELEMENTAL && problem->face_adj) {
1468 Gen_Error(1, "WARNING: can only use face definition of");
1469 Gen_Error(1, "WARNING: adjacency with elemental decomposition");
1470 Gen_Error(1, "WARNING: face definition turned off");
1471 problem->face_adj = 0;
1472 }
1473
1474 /*
1475 * Detecting columns and fixing their partitioning only makes sense
1476 * with an elemental decomposition
1477 */
1478 if (problem->type != DecompType::ELEMENTAL && problem->fix_columns) {
1479 Gen_Error(1, "WARNING: can only use fix columns options");
1480 Gen_Error(1, "WARNING: with elemental decomposition");
1481 Gen_Error(1, "WARNING: fix columns option turned off");
1482 problem->fix_columns = 0;
1483 }
1484
1485 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1486 /* Check the load balance parameters */
1487 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1488 if (lb->type != Balance::MULTIKL && lb->type != Balance::SPECTRAL &&
1489 lb->type != Balance::INERTIAL && lb->type != Balance::LINEAR && lb->type != Balance::RANDOM &&
1490 lb->type != Balance::SCATTERED && lb->type != Balance::INFILE &&
1491 lb->type != Balance::ZPINCH && lb->type != Balance::BRICK &&
1492 lb->type != Balance::ZOLTAN_RCB && lb->type != Balance::ZOLTAN_RIB &&
1493 lb->type != Balance::ZOLTAN_HSFC) {
1494 Gen_Error(0, "FATAL: unknown load balance type requested");
1495 return 0;
1496 }
1497
1498 if ((sizeof(INT) == 8) && (lb->type != Balance::LINEAR && lb->type != Balance::SCATTERED)) {
1499 Gen_Error(1, "WARNING: This mesh is using 64-bit integers. The only supported");
1500 Gen_Error(1, " load balance methods for 64-bit integers are 'linear' or 'scattered'");
1501 Gen_Error(1, " The load balance method will be automatically changed to 'linear'.");
1502 lb->type = Balance::LINEAR;
1503 }
1504
1505 if (lb->type == Balance::MULTIKL) {
1506 lb->refine = Balance::KL_REFINE;
1507 }
1508
1509 if (lb->refine != Balance::KL_REFINE && lb->refine != Balance::NO_REFINE) {
1510 lb->refine = Balance::NO_REFINE; /* Default if not specified */
1511 }
1512
1513 if (lb->num_sects <= 0) {
1514 lb->num_sects = 1; /* Default if not specified */
1515 }
1516
1517 if (lb->cnctd_dom < 0) {
1518 lb->cnctd_dom = 0;
1519 }
1520 else if (!problem->face_adj) {
1521 Gen_Error(1, "WARNING: can only set connected domain");
1522 Gen_Error(1, "WARNING: when using face definition of adjacency");
1523 Gen_Error(1, "WARNING: connected domain turned off");
1524 lb->cnctd_dom = 0;
1525 }
1526
1527 if (lb->outfile < 0) {
1528 lb->outfile = ELB_FALSE;
1529 }
1530
1531 if (lb->type == Balance::INFILE) {
1532 if (lb->outfile) {
1533 Gen_Error(0, "FATAL: both infile and outfile cannot be specified");
1534 return 0;
1535 }
1536
1537 if (lb->refine != Balance::NO_REFINE) {
1538 Gen_Error(1, "WARNING: no refinement can be specified with infile");
1539 return 0;
1540 }
1541 }
1542
1543 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1544 /* Check the eigensolver parameters */
1545 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1546 if (lb->type == Balance::SPECTRAL || lb->type == Balance::MULTIKL) {
1547 if (solver->tolerance < 0.0) {
1548 Gen_Error(1, "WARNING: using default value for eigensolver"
1549 " tolerance");
1550 solver->tolerance = 1.0e-3;
1551 }
1552
1553 if (solver->vmax < 0) {
1554 Gen_Error(1, "WARNING: no value for vmax specified,"
1555 " using default");
1556 solver->vmax = 200;
1557 }
1558 }
1559
1560 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1561 /* Check the output file name */
1562 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1563 if (nemI_out_file.empty()) {
1564 /*
1565 * Generate the file name from the input file name and the requested
1566 * load balance method.
1567 */
1568 std::string ctemp2;
1569 switch (machine->type) {
1570 case MachineType::MESH: ctemp2 = fmt::format("-m{}-", machine->num_procs); break;
1571
1572 case MachineType::HCUBE: ctemp2 = fmt::format("-h{}-", machine->num_procs); break;
1573 default:; // do nothing
1574 }
1575
1576 switch (lb->type) {
1577 case Balance::MULTIKL:
1578 case Balance::SPECTRAL:
1579 if (lb->num_sects == 1) {
1580 ctemp2 += "b";
1581 }
1582 else if (lb->num_sects == 2) {
1583 ctemp2 += "q";
1584 }
1585 else if (lb->num_sects == 3) {
1586 ctemp2 += "o";
1587 }
1588
1589 break;
1590
1591 case Balance::INERTIAL: ctemp2 += "i"; break;
1592
1593 case Balance::ZPINCH: ctemp2 += "z"; break;
1594
1595 case Balance::BRICK: ctemp2 += "x"; break;
1596
1597 case Balance::ZOLTAN_RCB:
1598 case Balance::ZOLTAN_RIB:
1599 case Balance::ZOLTAN_HSFC: ctemp2 += "Z"; break;
1600
1601 case Balance::SCATTERED: ctemp2 += "s"; break;
1602
1603 case Balance::RANDOM: ctemp2 += "r"; break;
1604
1605 case Balance::LINEAR: ctemp2 += "l"; break;
1606 default:; // do nothing
1607 }
1608
1609 if (lb->refine == Balance::KL_REFINE) {
1610 ctemp2 += "KL";
1611 }
1612
1613 /* Generate the complete file name */
1614
1615 nemI_out_file = remove_extension(exoII_inp_file);
1616 nemI_out_file += ctemp2;
1617 nemI_out_file += ".nemI";
1618 } /* End "if(strlen(nemI_out_file) <= 0)" */
1619
1620 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1621 /* Check the weighting specifications */
1622 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1623 if (weight->exo_filename.length() > 0) {
1624 /* Check that a variable name and/or index was specified. */
1625 if (weight->exo_varname.empty() && weight->exo_vindx <= 0) {
1626 Gen_Error(0, "FATAL: must specify an index and/or a name for weighting"
1627 " variable");
1628 return 0;
1629 }
1630
1631 /*
1632 * If a variable name and or index was specified then open the ExodusII
1633 * file and compare the specified name against what exists in the file.
1634 */
1635 int exoid;
1636 float version;
1637 int cpu_ws = 0;
1638 int io_ws = 0;
1639 if ((exoid = ex_open(weight->exo_filename.c_str(), EX_READ, &cpu_ws, &io_ws, &version)) < 0) {
1640 std::string ctemp =
1641 fmt::format("FATAL: failed to open ExodusII weighting file {}", weight->exo_filename);
1642 Gen_Error(0, ctemp);
1643 return 0;
1644 }
1645
1646 int ntimes = ex_inquire_int(exoid, EX_INQ_TIME);
1647
1648 /* Check the time index */
1649 if (weight->exo_tindx <= 0) {
1650 weight->exo_tindx = 1; /* Defaults to 1 */
1651 }
1652
1653 if (weight->exo_tindx > ntimes) {
1654 std::string ctemp = fmt::format(
1655 "FATAL: requested time index %d not available in weighting file", weight->exo_tindx);
1656 Gen_Error(0, ctemp);
1657 return 0;
1658 }
1659
1660 ex_entity_type type;
1661 if (problem->type == DecompType::NODAL) {
1662 type = EX_NODAL;
1663 }
1664 else {
1665 type = EX_ELEM_BLOCK;
1666 }
1667
1668 /*
1669 * First check that there are variables of the requested type in the
1670 * specified ExodusII file.
1671 */
1672 int nvars;
1673 if (ex_get_variable_param(exoid, type, &nvars) < 0) {
1674 Gen_Error(0, "FATAL: unable to get variable params from ExodusII"
1675 " weighting file");
1676 return 0;
1677 }
1678 if (nvars <= 0) {
1679 Gen_Error(0, "FATAL: no variables found in the ExodusII weighting file");
1680 return 0;
1681 }
1682
1683 /* Read the variable names from the requested file */
1684 char **var_names = reinterpret_cast<char **>(malloc(nvars * sizeof(char *)));
1685 if (!var_names) {
1686 Gen_Error(0, "FATAL: insufficient memory");
1687 return 0;
1688 }
1689
1690 {
1691 int max_name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_USED_NAME_LENGTH);
1692 ex_set_max_name_length(exoid, max_name_length);
1693
1694 for (int cnt = 0; cnt < nvars; cnt++) {
1695 var_names[cnt] = reinterpret_cast<char *>(malloc((max_name_length + 1) * sizeof(char)));
1696 if (!var_names[cnt]) {
1697 Gen_Error(0, "FATAL: insufficient memory");
1698 return 0;
1699 }
1700 }
1701 }
1702
1703 if (ex_get_variable_names(exoid, type, nvars, var_names) < 0) {
1704 Gen_Error(0, "FATAL: unable to obtain variable names for weighting");
1705 return 0;
1706 }
1707
1708 /*
1709 * If there was a variable name specified but no index then get the
1710 * index. If a variable name AND an index were specified then make
1711 * sure they match.
1712 */
1713 if (!weight->exo_varname.empty()) {
1714 int tmp_vindx = 0;
1715 for (int cnt = 0; cnt < nvars; cnt++) {
1716 if (strcmp(var_names[cnt], weight->exo_varname.c_str()) == 0) {
1717 tmp_vindx = cnt + 1;
1718
1719 break; /* out of "for" loop */
1720 }
1721 }
1722
1723 if (weight->exo_vindx <= 0) {
1724 weight->exo_vindx = tmp_vindx;
1725 }
1726 else if (weight->exo_vindx != tmp_vindx) {
1727 Gen_Error(0, "FATAL: requested weight variable index doesn't match"
1728 " the variable name in the ExodusII file");
1729 return 0;
1730 }
1731 }
1732
1733 /* Free up memory */
1734 for (int cnt = 0; cnt < nvars; cnt++) {
1735 free(var_names[cnt]);
1736 }
1737 free(var_names);
1738
1739 /*
1740 * If there is still no valid index then the variable name does
1741 * not exist in the specified file.
1742 */
1743 if (weight->exo_vindx <= 0) {
1744 std::string ctemp = fmt::format(
1745 "FATAL: requested weighting variable {} not found in ExodusII file", weight->exo_varname);
1746 Gen_Error(0, ctemp);
1747 return 0;
1748 }
1749
1750 if (nvars < weight->exo_vindx) {
1751 Gen_Error(0, "FATAL: requested variable index is larger than number in"
1752 " ExodusII weighting file");
1753 return 0;
1754 }
1755
1756 ex_close(exoid);
1757
1758 } /* End "if(strlen(weight->exo_filename) > 0)" */
1759
1760 if ((weight->type & EL_BLK) && (weight->ow_read)) {
1761 if (weight->elemblk.size() > 1) {
1762 /* start by sorting the two arrays by the element block number */
1763 sort2(weight->elemblk.size(), Data(weight->elemblk), Data(weight->elemblk_wgt));
1764
1765 /* now loop through, and make sure that we don't have multiple values */
1766 for (int cnt = 1; cnt < (int)weight->elemblk.size(); cnt++) {
1767 if (weight->elemblk[cnt] == weight->elemblk[cnt - 1]) {
1768 std::string ctemp =
1769 fmt::format("WARNING: multiple weight specified for block {}", weight->elemblk[cnt]);
1770 Gen_Error(1, ctemp);
1771 }
1772 }
1773 }
1774 } /* if ((weight->type & EL_BLK) && (weight->ow_read)) */
1775
1776 return 1;
1777
1778 } /*-------------------End check_inp_specs()-----------------*/
1779
1780 namespace {
1781 /* Parse comma separate list into words.
1782 Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc.
1783 This file is part of the GNU C Library.
1784 Contributed by Ulrich Drepper <[email protected]>, 1996.
1785 The GNU C Library is free software; you can redistribute it and/or
1786 modify it under the terms of the GNU Lesser General Public
1787 License as published by the Free Software Foundation; either
1788 version 2.1 of the License, or (at your option) any later version.
1789 The GNU C Library is distributed in the hope that it will be useful,
1790 but WITHOUT ANY WARRANTY; without even the implied warranty of
1791 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1792 Lesser General Public License for more details.
1793 You should have received a copy of the GNU Lesser General Public
1794 License along with the GNU C Library; if not, write to the Free
1795 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1796 02111-1307 USA. */
1797
1798 const char *my_strchrnul(const char *s, int c)
1799 {
1800 const char *result = strchr(s, c);
1801
1802 if (!result) {
1803 result = (char *)s + strlen(s);
1804 }
1805
1806 return (result);
1807 }
1808
1809 /* Parse comma separated suboption from *OPTIONP and match against
1810 strings in TOKENS. If found return index and set *VALUEP to
1811 optional value introduced by an equal sign. If the suboption is
1812 not part of TOKENS return in *VALUEP beginning of unknown
1813 suboption. On exit *OPTIONP is set to the beginning of the next
1814 token or at the terminating NUL character. */
1815 int my_getsubopt(char **optionp, char *const *tokens, char **valuep)
1816 {
1817 if (**optionp == '\0') {
1818 return -1;
1819 }
1820
1821 /* Find end of next token. */
1822 char *endp = (char *)my_strchrnul(*optionp, ',');
1823
1824 /* Find start of value. */
1825 char *vstart = (char *)memchr(*optionp, '=', endp - *optionp);
1826 if (vstart == nullptr) {
1827 vstart = endp;
1828 }
1829
1830 /* Try to match the characters between *OPTIONP and VSTART against
1831 one of the TOKENS. */
1832 for (int cnt = 0; tokens[cnt] != nullptr; ++cnt) {
1833 if (strncmp(*optionp, tokens[cnt], vstart - *optionp) == 0 &&
1834 tokens[cnt][vstart - *optionp] == '\0') {
1835 /* We found the current option in TOKENS. */
1836 *valuep = vstart != endp ? vstart + 1 : nullptr;
1837
1838 if (*endp != '\0') {
1839 *endp++ = '\0';
1840 }
1841 *optionp = endp;
1842
1843 return cnt;
1844 }
1845 }
1846
1847 /* The current suboption does not match any option. */
1848 *valuep = *optionp;
1849
1850 if (*endp != '\0') {
1851 *endp++ = '\0';
1852 }
1853 *optionp = endp;
1854
1855 return -1;
1856 }
1857
1858 void print_usage()
1859 {
1860 fmt::print("\nusage:\t{} [-h] [<-n|-e> -o <output file>"
1861 " -m <machine description>\n"
1862 "\t -l <load bal description> -s <eigen solver specs>\n"
1863 "\t -w <weighting options> -g <group list> -f]\n"
1864 "\t [-a <ascii file>] [-c change_set_#] exoII_file\n\n"
1865 " -C index\tSpecify which change set to read from (1-based index)\n"
1866 " -32\t\tforce use of 32-bit integers\n"
1867 " -64\t\tforce use of 64-bit integers\n"
1868 " -n\t\tperform a nodal based load balance\n"
1869 " -e\t\tperform an elemental based load balance\n"
1870 " -o NemI file\toutput NemesisI load-balance file name\n"
1871 " -m mach desc\tdescription of the machine to load balance for\n"
1872 " -l LB data\tload balance specifications\n"
1873 " -s Eigen specs\tEigen solver specifications\n"
1874 " -w weighting\tweighting specs for load balance\n"
1875 " -g groupings\tgrouping specifications for load balance\n"
1876 " -f\t\tuse face definition of adjacency\n"
1877 " -p\t\tuse partial definition of adjacency: \n"
1878 " \t\trequire only 3 matching quad face nodes\n"
1879 " -I\t\tavoid splitting vertical element columns\n"
1880 " \t\tacross partitions\n"
1881 " -h\t\tusage information\n"
1882 " -a ascii file\tget info from ascii input file name\n",
1883 UTIL_NAME);
1884 }
1885 } // namespace