def init(args: Optional[argparse.Namespace] = None): """Initialize a simulation and read configuration. Attributes ---------- args : None or argparse.Namespace By default, None means getting arguments from command-line. If not None, it should be the return from ArgumentParser.parse(). Returns: -------- config : a torchswe.utils.config.Config A Config instance holding a case's simulation configurations. All paths are converted to absolute paths. The temporal scheme is replaced with the corresponding function. grid : torch.utils.data.Gridlines Contains gridline coordinates. topo : torch.utils.data.Topography Contains topography elevation data. state_ic : torchswe.utils.data.WHUHVModel Initial confitions. """ # get cmd arguments if args is None: args = get_cmd_arguments() args.case_folder = args.case_folder.expanduser().resolve() args.yaml = args.case_folder.joinpath("config.yaml") # read yaml config file with open(args.yaml, "r") as fobj: config = yaml.load(fobj, yaml.Loader) assert isinstance(config, Config), \ "Failed to parse {} as an Config object. ".format(args.yaml) + \ "Check if `--- !Config` appears in the header of the YAML" # add args to config config.case = args.case_folder config.dtype = "float32" if args.sp else "float64" if args.log_steps is not None: config.params.log_steps = args.log_steps if args.tm is not None: # overwrite the setting in config.yaml config.temporal.scheme = args.tm # if topo filepath is relative, change to abs path config.topo.file = config.topo.file.expanduser() if not config.topo.file.is_absolute(): config.topo.file = config.case.joinpath(config.topo.file).resolve() # if ic filepath is relative, change to abs path if config.ic.file is not None: config.ic.file = config.ic.file.expanduser() if not config.ic.file.is_absolute(): config.ic.file = config.case.joinpath(config.ic.file).resolve() # if filepath of the prehook script is relative, change to abs path if config.prehook is not None: config.prehook = config.prehook.expanduser() if not config.prehook.is_absolute(): config.prehook = config.case.joinpath(config.prehook).resolve() # spatial discretization + output time values grid = get_gridlines( *config.spatial.discretization, *config.spatial.domain, get_snapshot_times(config.temporal.output[0], config.temporal.output[1:], config.temporal.dt), config.dtype) # topography topo = get_topography(config.topo.file, config.topo.key, grid.x.vert, grid.y.vert, config.dtype) # initial conditions state_ic = create_ic(config.ic, grid, topo, config.dtype) return config, grid, topo, state_ic
import numpy from torchswe.utils.data import get_gridlines from torchswe.utils.io import create_topography_file def main(): """Main function""" # pylint: disable=invalid-name case = pathlib.Path(__file__).expanduser().resolve().parent with open(case.joinpath("config.yaml"), 'r') as f: config = yaml.load(f, Loader=yaml.Loader) # gridlines; ignore temporal axis grid = get_gridlines(*config.spatial.discretization, *config.spatial.domain, [], config.dtype) # topography, defined on cell vertices B = numpy.tile(5. * numpy.exp(-0.4 * ((grid.x.vert - 5.)**2)), (grid.y.n + 1, 1)) create_topography_file(case.joinpath(config.topo.file), [grid.x.vert, grid.y.vert], B) return 0 if __name__ == "__main__": import sys sys.exit(main())