cmds.py 2.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import os
import sys
import shutil
import subprocess

try: # Python 2.x compatibility
    input = raw_input
except NameError:
    pass

# paths set by cmake
Santiago Ospina's avatar
Santiago Ospina committed
12
DORIEDIR = "/home/saospina/Codes/DUNE_INSTALL/Release/dorie"
13
MPIEXEC = "/usr/bin/mpiexec"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
MPIEXEC_NUMPROC_FLAG = "-n"
MPIEXEC_PREFLAG = ""
MPIEXEC_POSTFLAGS = ""

# set some paths
DORIE_EXECUTABLE = os.path.join(DORIEDIR, "dune/dorie/dorie")
PARAMETERDIR = os.path.join(DORIEDIR, "doc/default_files")
def MPIRUN(nproc,exe,*args,**kwargs):
    mpi_flags = kwargs.get("mpi_flags") or []
    return [k for k in [MPIEXEC,MPIEXEC_NUMPROC_FLAG,str(nproc)] \
                        + mpi_flags + [MPIEXEC_PREFLAG,str(exe),
                        MPIEXEC_POSTFLAGS] + list(args) if k]

def run(args):
    if not os.path.isfile(args["config"]):
        raise IOError("Configuration file {} not found".format(args["config"]))
    try:
        if args["parallel"] == 1:
            subprocess.check_call([DORIE_EXECUTABLE, args["config"]])
        else:
            subprocess.check_call(MPIRUN(args["parallel"], DORIE_EXECUTABLE,
                                         args["config"], mpi_flags=args["mpi_flags"]))
    except subprocess.CalledProcessError:
        print("Error while running DORiE")
        sys.exit(1)

def create(args):
    for f in ("config.ini", "parfield.ini", "2d_infiltr.bcdat", "3d_infiltr.bcdat"):
        newfile = os.path.join(os.getcwd(),f)
        if os.path.exists(newfile) and not args["force"]:
            override = (input("File {} exists. Override? [Y/n] ".format(f)) or "Y") in ["Y","y"]
            if not override:
                continue
        shutil.copyfile(os.path.join(PARAMETERDIR, f), newfile)
    print("Folder initialized")

def pfg(args):
    if not os.path.isfile(args["config"]):
        raise IOError("Configuration file {} not found".format(args["config"]))
    try:
        if args["parallel"] == 1:
            subprocess.check_call(["pf_from_file.py", args["config"]])
        else:
            if args["mpi_flags"]:
                mpi_flags = ["--mpi-flags=" + f for f in args["mpi_flags"]]
            else:
                mpi_flags = []
            subprocess.check_call(["pf_from_file.py", args["config"],
                                  "--parallel", str(args["parallel"])] + mpi_flags)
    except subprocess.CalledProcessError:
        print("Error while running DORiE")
        sys.exit(1)

def plot(args):
    if not os.path.isfile(args["vtk"]):
        raise IOError("File {} not found".format(args["vtk"]))
    try:
        if args["var"]:
            subprocess.check_call(["plot_vtk.py", "-f", args["vtk"], "--var", args["var"]])
        else:
            subprocess.check_call(["plot_vtk.py", "-f", args["vtk"]])
    except subprocess.CalledProcessError:
        print("Error while running DORiE")
        sys.exit(1)