|
| 1 | +# ------------------------------------------------------------------------------------------------------------ |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-only |
| 3 | +# |
| 4 | +# Copyright (c) 2016-2024 Lawrence Livermore National Security LLC |
| 5 | +# Copyright (c) 2018-2024 Total, S.A |
| 6 | +# Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University |
| 7 | +# Copyright (c) 2023-2024 Chevron |
| 8 | +# Copyright (c) 2019- GEOS/GEOSX Contributors |
| 9 | +# Copyright (c) 2019- INRIA project-team Makutu |
| 10 | +# All rights reserved |
| 11 | +# |
| 12 | +# See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details. |
| 13 | +# ------------------------------------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +import sys |
| 16 | +import argparse |
| 17 | + |
| 18 | +class GeosxArgs: |
| 19 | + """ |
| 20 | + Class containing the main argument in command line for GEOSX |
| 21 | + |
| 22 | + Attributes |
| 23 | + ---------- |
| 24 | + cmdline : list of str |
| 25 | + List corresponding to a splitted GEOSX submission line |
| 26 | + options : dict |
| 27 | + Dict containing GEOSX main options |
| 28 | + """ |
| 29 | + def __init__(self, args=sys.argv.copy()): |
| 30 | + """ |
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + args : list of str |
| 34 | + List corresponding to a splitted submission line with options |
| 35 | + """ |
| 36 | + self.cmdline = args |
| 37 | + self.options = self.optionsParser() |
| 38 | + |
| 39 | + |
| 40 | + def optionsParser(self, cmdline=[]): |
| 41 | + """ |
| 42 | + Return a dict with useful geosx options parsed from a list/submission line. |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + cmdline : list of str |
| 47 | + List corresponding to a splitted GEOSX submission line |
| 48 | +
|
| 49 | + Returns |
| 50 | + -------- |
| 51 | + dict : |
| 52 | + Dict containing GEOSX main options |
| 53 | + """ |
| 54 | + if not cmdline: |
| 55 | + cmdline = self.cmdline |
| 56 | + desc = "Parser of GEOSX command line" |
| 57 | + parser = argparse.ArgumentParser("GEOSX command line parser", |
| 58 | + allow_abbrev=False, |
| 59 | + description=desc) |
| 60 | + |
| 61 | + parser.add_argument("--input", "-i", "--xml", |
| 62 | + type=str, dest="xml", default=None, |
| 63 | + help="XML input file") |
| 64 | + parser.add_argument("--restart", "-r", |
| 65 | + dest="restart", type=str, default=None, |
| 66 | + help="Target restart filename") |
| 67 | + parser.add_argument("-x-partitions", "-x", |
| 68 | + type=int, dest="xpart", default=None, |
| 69 | + help="Number of partitions in the x direction.") |
| 70 | + parser.add_argument("-y-partitions", "-y", |
| 71 | + type=int, dest="ypart", default=None, |
| 72 | + help="Number of partitions in the y direction.") |
| 73 | + parser.add_argument("-z-partitions", "-z", |
| 74 | + type=int, dest="zpart", default=None, |
| 75 | + help="Number of partitions in the z direction.") |
| 76 | + parser.add_argument("--output", "-o", |
| 77 | + type=str, dest="outputdir", default=None, |
| 78 | + help="Directory to put the output files") |
| 79 | + parser.add_argument("--use-nonblocking", "-b", default=None, |
| 80 | + dest="non_blocking", action="store_true", |
| 81 | + help="Use non-blocking MPI communication") |
| 82 | + parser.add_argument("--name", "-n", |
| 83 | + dest="name", default=None, |
| 84 | + help="Name of the problem, used for output") |
| 85 | + parser.add_argument("--suppress-pinned", "-s", |
| 86 | + dest="supp_pinned", action="store_true", default=None, |
| 87 | + help="Suppress usage of pinned memory for MPI communication buffers") |
| 88 | + parser.add_argument("--timers", "-t", |
| 89 | + type=str, dest="timers", default=None, |
| 90 | + help="String specifying the type of timer output") |
| 91 | + parser.add_argument("--trace-data-migration", |
| 92 | + dest="trace_data_mig", action="store_true", default=None, |
| 93 | + help="Trace host-device data migration") |
| 94 | + parser.add_argument("--pause-for", |
| 95 | + dest="pause_for", default=None, |
| 96 | + help="Pause geosx for a given number of seconds before starting execution") |
| 97 | + |
| 98 | + return vars(parser.parse_known_args(cmdline)[0]) |
| 99 | + |
| 100 | + |
| 101 | + def updateArg(self, optionName=None, newValue=None): |
| 102 | + """ |
| 103 | + Update the GEOSX initialization arguments |
| 104 | +
|
| 105 | + Parameters |
| 106 | + ---------- |
| 107 | + optionName : str |
| 108 | + Name of the option to update |
| 109 | + newValue : str |
| 110 | + New value for the argument to be updated. |
| 111 | + |
| 112 | + Returns |
| 113 | + ------- |
| 114 | + bool : True if the option has been (found and) updated. False otherwise. |
| 115 | + """ |
| 116 | + if optionName is not None: |
| 117 | + if self.options[optionName] != newValue: |
| 118 | + self.options.update({optionName: newValue}) |
| 119 | + return True |
| 120 | + |
| 121 | + return False |
| 122 | + |
| 123 | + |
| 124 | + def getCommandLine(self): |
| 125 | + """ |
| 126 | + Return the command line specific to GEOSX initialization |
| 127 | +
|
| 128 | + Returns |
| 129 | + -------- |
| 130 | + cl : list of str |
| 131 | + List containing all the options requested |
| 132 | + """ |
| 133 | + cl = [""] |
| 134 | + for opt, val in self.options.items(): |
| 135 | + if val is not None: |
| 136 | + ab = GeosxAbbrevOption().getAbbrv(opt) |
| 137 | + cl += [ab] |
| 138 | + if not isinstance(self.options[opt], bool): |
| 139 | + cl += [str(self.options[opt])] |
| 140 | + |
| 141 | + return cl |
| 142 | + |
| 143 | + |
| 144 | +class GeosxAbbrevOption: |
| 145 | + """ |
| 146 | + Class containing GEOSX command line options abbreviations |
| 147 | + |
| 148 | + Attributes |
| 149 | + ---------- |
| 150 | + abbrvDict : dict |
| 151 | + Dict containing lists of abbreviations for GEOSX command line options |
| 152 | + """ |
| 153 | + def __init__(self): |
| 154 | + self.abbrvDict = {"xml": ("--input", "-i"), |
| 155 | + "restart": ("--restart", "r"), |
| 156 | + "xpart": ("-x-partitions", "-x"), |
| 157 | + "ypart": ("-y-partitions", "-y"), |
| 158 | + "zpart": ("-z-partitions", "-z"), |
| 159 | + "non_blocking": ("--use-non-blocking", "-b"), |
| 160 | + "name": ("--name", "-n"), |
| 161 | + "supp_pinned": ("--suppress-pinned"), |
| 162 | + "outputdir": ("--output", "-o"), |
| 163 | + "timers": ("--timers", "-t"), |
| 164 | + "trace_data_mig": ("--trace-data-migration"), |
| 165 | + "pause_for": ("--pause-for") |
| 166 | + } |
| 167 | + |
| 168 | + def getAbbrv(self, optionName=None): |
| 169 | + """ |
| 170 | + Returns the abbreviation corresponding to the requested option |
| 171 | + |
| 172 | + Parameters |
| 173 | + ---------- |
| 174 | + optionName : str |
| 175 | + Name of the requested option |
| 176 | +
|
| 177 | + Returns |
| 178 | + ------- |
| 179 | + str : Requested abbreviations |
| 180 | + """ |
| 181 | + return self.abbrvDict[optionName][-1] |
| 182 | + |
| 183 | + |
| 184 | + def getAllAbbrv(self, optionName=None): |
| 185 | + """ |
| 186 | + Returns the abbreviation corresponding to the requested option |
| 187 | + |
| 188 | + Parameters |
| 189 | + ---------- |
| 190 | + optionName : str |
| 191 | + Name of the requested option |
| 192 | +
|
| 193 | + Returns |
| 194 | + ------- |
| 195 | + list of str : |
| 196 | + Requested abbreviations |
| 197 | + """ |
| 198 | + try: |
| 199 | + return self.abbrvDict[optionName] |
| 200 | + |
| 201 | + except: |
| 202 | + return [""] |
0 commit comments