Skip to content
Snippets Groups Projects
black-hole-accretion.py 53.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Angle with normal to disc 10 degrees
    
        Angle = numpy.pi / 180.0 * (90.0 - 10.0)
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Radiation of disc : BlackBody or Monochromatic
        BlackBody = False
        # Size of image
    
        Size = 1024
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Variable Type
    
        VariableType = "FP32"
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # ?
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Method of resolution
    
        Method = "TrajectoPixel"
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Colors for output image
    
        Colors = "Greyscale"
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Physics
    
        Physics = "Einstein"
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # No output as image
        NoImage = False
        # Threads in CUDA
        Threads = 32
        # Trackpoints of trajectories
    
        TrackPoints = 2048
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        # Tracksave of trajectories
    
        TrackSave = False
    
        HowToUse = "%s -h [Help] -b [BlackBodyEmission] -j [TrackSave] -n [NoImage] -p <Einstein/Newton> -s <SizeInPixels> -m <Mass> -i <DiscInternalRadius> -x <DiscExternalRadius> -a <AngleAboveDisc> -d <DeviceId> -c <Greyscale/Red2Yellow> -g <CUDA/OpenCL> -o <EachPixel/TrajectoCircle/TrajectoPixel/EachCircle/Original> -t <ThreadsInCuda> -v <FP32/FP64> -k <TrackPoints>"  # noqa: E501
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
    
        try:
    
            opts, args = getopt.getopt(
                sys.argv[1:],
                "hbnjs:m:i:x:a:d:g:v:o:t:c:p:k:",
                [
                    "tracksave",
                    "blackbody",
                    "noimage",
                    "camera",
                    "size=",
                    "mass=",
                    "internal=",
                    "external=",
                    "angle=",
                    "device=",
                    "gpustyle=",
                    "variabletype=",
                    "method=",
                    "threads=",
                    "colors=",
                    "physics=",
                    "trackpoints=",
                ],
            )
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        except getopt.GetoptError:
            print(HowToUse % sys.argv[0])
            sys.exit(2)
    
        # List of Devices
    
        Devices = []
        Alu = {}
    
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        for opt, arg in opts:
    
            if opt == "-h":
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                print(HowToUse % sys.argv[0])
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                print("\nInformations about devices detected under OpenCL API:")
                # For PyOpenCL import
                try:
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                    for platform in cl.get_platforms():
                        for device in platform.get_devices():
    
                            # deviceType=cl.device_type.to_string(device.type)
                            deviceType = "xPU"
                            print(
                                "Device #%i from %s of type %s : %s"
                                % (
                                    Id,
                                    platform.vendor.lstrip(),
                                    deviceType,
                                    device.name.lstrip(),
                                )
                            )
                            Id = Id + 1
    
                except Exception:
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                    print("Your platform does not seem to support OpenCL")
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                print("\nInformations about devices detected under CUDA API:")
    
                # For PyCUDA import
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                try:
                    import pycuda.driver as cuda
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                    cuda.init()
                    for Id in range(cuda.Device.count()):
    
                        device = cuda.Device(Id)
                        print("Device #%i of type GPU : %s" % (Id, device.name()))
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                    print
    
                except Exception:
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                    print("Your platform does not seem to support CUDA")
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                sys.exit()
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            elif opt in ("-d", "--device"):
    
                #            Devices.append(int(arg))
                Device = int(arg)
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            elif opt in ("-g", "--gpustyle"):
                GpuStyle = arg
            elif opt in ("-v", "--variabletype"):
                VariableType = arg
            elif opt in ("-s", "--size"):
                Size = int(arg)
            elif opt in ("-k", "--trackpoints"):
                TrackPoints = int(arg)
            elif opt in ("-m", "--mass"):
                Mass = float(arg)
            elif opt in ("-i", "--internal"):
                InternalRadius = float(arg)
            elif opt in ("-e", "--external"):
                ExternalRadius = float(arg)
            elif opt in ("-a", "--angle"):
    
                Angle = numpy.pi / 180.0 * (90.0 - float(arg))
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            elif opt in ("-b", "--blackbody"):
                BlackBody = True
            elif opt in ("-j", "--tracksave"):
                TrackSave = True
            elif opt in ("-n", "--noimage"):
                NoImage = True
            elif opt in ("-o", "--method"):
                Method = arg
            elif opt in ("-t", "--threads"):
                Threads = int(arg)
            elif opt in ("-c", "--colors"):
                Colors = arg
            elif opt in ("-p", "--physics"):
                Physics = arg
    
        print("Device Identification selected : %s" % Device)
        print("GpuStyle used : %s" % GpuStyle)
        print("VariableType : %s" % VariableType)
        print("Size : %i" % Size)
        print("Mass : %f" % Mass)
        print("Internal Radius : %f" % InternalRadius)
        print("External Radius : %f" % ExternalRadius)
        print("Angle with normal of (in radians) : %f" % Angle)
        print("Black Body Disc Emission (monochromatic instead) : %s" % BlackBody)
        print("Method of resolution : %s" % Method)
        print("Colors for output images : %s" % Colors)
        print("Physics used for Trajectories : %s" % Physics)
        print("Trackpoints of Trajectories : %i" % TrackPoints)
        print("Tracksave of Trajectories : %i" % TrackSave)
    
    
        if GpuStyle == "CUDA":
            print("\nSelection of CUDA device")
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            try:
                # For PyCUDA import
                import pycuda.driver as cuda
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                cuda.init()
                for Id in range(cuda.Device.count()):
    
                    device = cuda.Device(Id)
                    print("Device #%i of type GPU : %s" % (Id, device.name()))
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                    if Id in Devices:
    
                        Alu[Id] = "GPU"
    
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            except ImportError:
                print("Platform does not seem to support CUDA")
    
    
        if GpuStyle == "OpenCL":
            print("\nSelection of OpenCL device")
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            try:
                # For PyOpenCL import
                import pyopencl as cl
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
                for platform in cl.get_platforms():
                    for device in platform.get_devices():
    
                        # deviceType=cl.device_type.to_string(device.type)
                        deviceType = "xPU"
                        print(
                            "Device #%i from %s of type %s : %s"
                            % (
                                Id,
                                platform.vendor.lstrip().rstrip(),
                                deviceType,
                                device.name.lstrip().rstrip(),
                            )
                        )
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
    
                        if Id in Devices:
    
                            # Set the Alu as detected Device Type
                            Alu[Id] = deviceType
                        Id = Id + 1
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
            except ImportError:
                print("Platform does not seem to support OpenCL")
    
    
        zImage = numpy.zeros((Size, Size), dtype=numpy.float32)
        fImage = numpy.zeros((Size, Size), dtype=numpy.float32)
    
        InputCL = {}
        InputCL["Device"] = Device
        InputCL["GpuStyle"] = GpuStyle
        InputCL["VariableType"] = VariableType
        InputCL["Size"] = Size
        InputCL["Mass"] = Mass
        InputCL["InternalRadius"] = InternalRadius
        InputCL["ExternalRadius"] = ExternalRadius
        InputCL["Angle"] = Angle
        InputCL["BlackBody"] = BlackBody
        InputCL["Method"] = Method
        InputCL["TrackPoints"] = TrackPoints
        InputCL["Physics"] = Physics
        InputCL["Threads"] = Threads
        InputCL["NoImage"] = NoImage
        InputCL["TrackSave"] = TrackSave
    
        if GpuStyle == "OpenCL":
            duration = BlackHoleCL(zImage, fImage, InputCL)
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        else:
    
            duration = BlackHoleCUDA(zImage, fImage, InputCL)
    
        Hostname = gethostname()
        Date = time.strftime("%Y%m%d_%H%M%S")
        ImageInfo = "%s_Device%i_%s_%s" % (Method, Device, Hostname, Date)
    
    
    Emmanuel QUEMENER's avatar
    Emmanuel QUEMENER committed
        if not NoImage:
    
            ImageOutput(zImage, "TrouNoirZ_%s" % ImageInfo, Colors)
            ImageOutput(fImage, "TrouNoirF_%s" % ImageInfo, Colors)