Esempio n. 1
0
    def test_innovations(self):
        self.set_positions_small_set()
        zero = 0.0
        close = 0.2
        far = -0.8

        fasit = [[close, close], [close, zero], [zero, close]]
        # smallDrifterSet is initially with periodic boundary conditions
        assert2DListAlmostEqual(self, self.smallDrifterSet.getInnovations().tolist(), \
                              fasit, 6,
                              'innovations with periodic boundaries')

        fasit = [[far, far], [far, zero], [zero, far]]
        self.smallDrifterSet.setBoundaryConditions(
            Common.BoundaryConditions(1, 1, 1, 1))
        assert2DListAlmostEqual(self, self.smallDrifterSet.getInnovations().tolist(), \
                              fasit, 6,
                              'innovations with non-periodic boundaries')

        fasit = [[close, far], [close, zero], [zero, far]]
        self.smallDrifterSet.setBoundaryConditions(
            Common.BoundaryConditions(1, 2, 1, 2))
        assert2DListAlmostEqual(self, self.smallDrifterSet.getInnovations().tolist(), \
                              fasit, 6,
                              'innovations with periodic boundaries in east-west')

        fasit = [[far, close], [far, zero], [zero, close]]
        self.smallDrifterSet.setBoundaryConditions(
            Common.BoundaryConditions(2, 1, 2, 1))
        assert2DListAlmostEqual(self, self.smallDrifterSet.getInnovations().tolist(), \
                              fasit, 6,
                              'innovations with periodic boundaries in north-south')
Esempio n. 2
0
    def test_distances(self):
        self.set_positions_small_set()
        longDiag = np.sqrt(2 * 0.8 * 0.8)
        longLine = 0.8
        shortDiag = np.sqrt(0.2 * 0.2 + 0.2 * 0.2)
        shortLine = 0.2
        semiDiag = np.sqrt(0.2 * 0.2 + 0.8 * 0.8)

        # smallDrifterSet is initially with periodic boundary conditions
        assertListAlmostEqual(self, self.smallDrifterSet.getDistances().tolist(), \
                              [shortDiag, shortLine, shortLine], 6,
                              'distance with periodic boundaries')

        self.smallDrifterSet.setBoundaryConditions(
            Common.BoundaryConditions(1, 1, 1, 1))
        assertListAlmostEqual(self, self.smallDrifterSet.getDistances().tolist(), \
                              [longDiag, longLine, longLine], 6,
                              'distances with non-periodic boundaries')

        self.smallDrifterSet.setBoundaryConditions(
            Common.BoundaryConditions(1, 2, 1, 2))
        assertListAlmostEqual(self, self.smallDrifterSet.getDistances().tolist(), \
                              [semiDiag, shortLine, longLine], 6,
                              'distances with periodic boundaries in east-west')

        self.smallDrifterSet.setBoundaryConditions(
            Common.BoundaryConditions(2, 1, 2, 1))
        assertListAlmostEqual(self, self.smallDrifterSet.getDistances().tolist(), \
                              [semiDiag, longLine, shortLine], 6,
                              'distances with periodic boundaries in north-south')
Esempio n. 3
0
    def fromfilename(cls, gpu_ctx, filename, cont_write_netcdf=True):
        """
        Initialize and hotstart simulation from nc-file.
        cont_write_netcdf: Continue to write the results after each superstep to a new netCDF file
        filename: Continue simulation based on parameters and last timestep in this file
        """
        # open nc-file
        sim_reader = SimReader.SimNetCDFReader(filename,
                                               ignore_ghostcells=False)
        sim_name = str(sim_reader.get('simulator_short'))
        assert sim_name == cls.__name__, \
               "Trying to initialize a " + \
               cls.__name__ + " simulator with netCDF file based on " \
               + sim_name + " results."

        # read parameters
        nx = sim_reader.get("nx")
        ny = sim_reader.get("ny")

        dx = sim_reader.get("dx")
        dy = sim_reader.get("dy")

        width = nx * dx
        height = ny * dy

        dt = sim_reader.get("dt")
        g = sim_reader.get("g")
        r = sim_reader.get("bottom_friction_r")
        f = sim_reader.get("coriolis_force")
        beta = sim_reader.get("coriolis_beta")

        minmodTheta = sim_reader.get("minmod_theta")
        timeIntegrator = sim_reader.get("time_integrator")
        y_zero_reference_cell = sim_reader.get("y_zero_reference_cell")

        try:
            wind_stress_type = sim_reader.get("wind_stress_type")
            wind = Common.WindStressParams(type=wind_stress_type)
        except:
            wind = WindStress.WindStress()

        boundaryConditions = Common.BoundaryConditions( \
            sim_reader.getBC()[0], sim_reader.getBC()[1], \
            sim_reader.getBC()[2], sim_reader.getBC()[3], \
            sim_reader.getBCSpongeCells())

        h0 = sim_reader.getH()

        # get last timestep (including simulation time of last timestep)
        eta0, hu0, hv0, time0 = sim_reader.getLastTimeStep()

        return cls(gpu_ctx, \
                h0, eta0, hu0, hv0, \
                nx, ny, \
                dx, dy, dt, \
                g, f, r, \
                t=time0, \
                wind_stress=wind, \
                boundary_conditions=boundaryConditions, \
                write_netcdf=cont_write_netcdf)
Esempio n. 4
0
 def setBoundaryConditions(self, bcSettings=1):
     if (bcSettings == 1):
         self.boundaryConditions = Common.BoundaryConditions()
     elif (bcSettings == 2):
         self.boundaryConditions = Common.BoundaryConditions(2, 2, 2, 2)
     elif bcSettings == 3:
         self.boundaryConditions = Common.BoundaryConditions(2, 1, 2, 1)
     else:
         self.boundaryConditions = Common.BoundaryConditions(1, 2, 1, 2)
Esempio n. 5
0
 def allocateBuffers(self, HCPU):
     host_buffer = np.zeros((self.ny, self.nx))
     self.eta = Common.CUDAArray2D(self.gpu_stream, self.nx, self.ny, 0, 0,
                                   host_buffer)
     self.hu = Common.CUDAArray2D(self.gpu_stream, self.nx, self.ny, 0, 0,
                                  host_buffer)
     self.hv = Common.CUDAArray2D(self.gpu_stream, self.nx, self.ny, 0, 0,
                                  host_buffer)
     self.H = Common.CUDAArray2D(self.gpu_stream, self.nx + 1, self.ny + 1,
                                 0, 0, HCPU)
     del host_buffer
Esempio n. 6
0
    def __init__(self, gpu_ctx, numDrifters, \
                 observation_variance=0.01, \
                 boundaryConditions=Common.BoundaryConditions(), \
                 initialization_cov_drifters=None, \
                 domain_size_x=1.0, domain_size_y=1.0, \
                 gpu_stream=None, \
                 block_width = 64):
        
        super(GPUDrifterCollection, self).__init__(numDrifters,
                                observation_variance=observation_variance,
                                boundaryConditions=boundaryConditions,
                                domain_size_x=domain_size_x, 
                                domain_size_y=domain_size_y)
        
        # Define CUDA environment:
        self.gpu_ctx = gpu_ctx
        self.block_width = block_width
        self.block_height = 1
        
        # TODO: Where should the cl_queue come from?
        # For sure, the drifter and the ocean simulator should use 
        # the same queue...
        self.gpu_stream = gpu_stream
        if self.gpu_stream is None:
            self.gpu_stream = cuda.Stream()
                
        self.sensitivity = 1.0
         
        self.driftersHost = np.zeros((self.getNumDrifters() + 1, 2)).astype(np.float32, order='C')
        self.driftersDevice = Common.CUDAArray2D(self.gpu_stream, \
                                                 2, self.getNumDrifters()+1, 0, 0, \
                                                 self.driftersHost)
        
        self.drift_kernels = gpu_ctx.get_kernel("driftKernels.cu", \
                                                defines={'block_width': self.block_width, 'block_height': self.block_height})

        # Get CUDA functions and define data types for prepared_{async_}call()
        self.passiveDrifterKernel = self.drift_kernels.get_function("passiveDrifterKernel")
        self.passiveDrifterKernel.prepare("iifffiiPiPiPifiiiPif")
        self.enforceBoundaryConditionsKernel = self.drift_kernels.get_function("enforceBoundaryConditions")
        self.enforceBoundaryConditionsKernel.prepare("ffiiiPi")
        
        self.local_size = (self.block_width, self.block_height, 1)
        self.global_size = (\
                            int(np.ceil((self.getNumDrifters() + 2)/float(self.block_width))), \
                            1)
        
        # Initialize drifters:
        self.uniformly_distribute_drifters(initialization_cov_drifters=initialization_cov_drifters)
Esempio n. 7
0
    def setUp(self):
        self.gpu_ctx = None

        self.numDrifters = 3
        self.observationVariance = 0.25
        self.boundaryCondition = Common.BoundaryConditions(2, 2, 2, 2)
        self.smallDrifterSet = None
        # to be initialized by child class with above values

        self.smallPositionSetHost = np.array([[0.9, 0.9], [0.9, 0.1],
                                              [0.1, 0.9], [0.1, 0.1]])

        self.resampleNumDrifters = 6
        self.resamplingDriftersArray = np.zeros((7, 2))
        for i in range(2):
            self.resamplingDriftersArray[3 * i + 0, :] = [0.25, 0.35 + i * 0.3]
            self.resamplingDriftersArray[3 * i + 1, :] = [0.4, 0.35 + i * 0.3]
            self.resamplingDriftersArray[3 * i + 2, :] = [0.65, 0.35 + i * 0.3]
        self.resamplingDriftersArray[6, :] = [0.25, 0.5]
        self.resamplingDrifterSet = None
        # to be initialized by child class wit resampleNumDrifters only.

        self.resamplingVar = 1e-8

        self.largeDrifterSet = None
Esempio n. 8
0
    def setUp(self):

        #Set which CL device to use, and disable kernel caching
        self.cl_ctx = make_cl_ctx()

        # Make some host data which we can play with
        self.nx = 3
        self.ny = 5
        self.nx_halo = 1
        self.ny_halo = 2
        self.dataShape = (self.ny + 2 * self.ny_halo,
                          self.nx + 2 * self.nx_halo)

        self.buf1 = np.zeros(self.dataShape, dtype=np.float32, order='C')
        self.buf2 = np.zeros(self.dataShape)
        self.buf3 = np.zeros(self.dataShape, dtype=np.float32, order='C')
        for j in range(self.dataShape[0]):
            for i in range(self.dataShape[1]):
                self.buf1[j, i] = i * 100 + j
                self.buf2[j, i] = self.buf1[j, i]
                self.buf3[j, i] = j * 1000 - i

        self.explicit_free = False

        self.device_name = self.cl_ctx.devices[0].name
        self.cl_queue = pyopencl.CommandQueue(self.cl_ctx)
        self.tests_failed = True

        self.clarray = Common.OpenCLArray2D(self.cl_ctx, \
                                            self.nx, self.ny, self.nx_halo, self.ny_halo, \
                                            self.buf1)
Esempio n. 9
0
    def __init__(self,
                 numDrifters,
                 observation_variance=0.01,
                 boundaryConditions=Common.BoundaryConditions(),
                 initialization_cov_drifters=None,
                 domain_size_x=1.0,
                 domain_size_y=1.0):
        """
        Creates a GlobalParticles object for drift trajectory ensemble.

        numDrifters: number of drifters in the collection, not included the observation
        observation_variance: uncertainty of observation position
        boundaryConditions: BoundaryConditions object, relevant during re-initialization of particles.    
        """

        # Call parent constructor
        super(CPUDrifterCollection,
              self).__init__(numDrifters,
                             observation_variance=observation_variance,
                             boundaryConditions=boundaryConditions,
                             domain_size_x=domain_size_x,
                             domain_size_y=domain_size_y)

        # One position for every particle plus observation
        self.positions = np.zeros((self.numDrifters + 1, 2))

        # Initialize drifters:
        self.uniformly_distribute_drifters(
            initialization_cov_drifters=initialization_cov_drifters)
Esempio n. 10
0
    def _setupGPU(self):
        """
        Setting up kernel for reading observations, along with host and device buffers
        """
        # Create observation buffer!
        if self.observation_type == dautils.ObservationType.UnderlyingFlow or \
            self.observation_type == dautils.ObservationType.DirectUnderlyingFlow:

            zeros = np.zeros((self.driftersPerOceanModel, 2),
                             dtype=np.float32,
                             order='C')
            self.observation_buffer = Common.CUDAArray2D(self.gpu_stream, \
                                                         2, self.driftersPerOceanModel, 0, 0, \
                                                         zeros)

            # Generate kernels
            self.observation_kernels = self.gpu_ctx.get_kernel("observationKernels.cu", \
                                                             defines={})

            # Get CUDA functions and define data types for prepared_{async_}call()
            self.observeUnderlyingFlowKernel = self.observation_kernels.get_function(
                "observeUnderlyingFlow")
            self.observeUnderlyingFlowKernel.prepare("iiffiiPiPiPifiPiPi")

            self.local_size = (int(self.driftersPerOceanModel), 1, 1)
            self.global_size = (1, 1)
Esempio n. 11
0
    def setUp(self):
        self.gpu_ctx = Common.CUDAContext()

        self.nx = 50
        self.ny = 70

        self.dx = 200.0
        self.dy = 200.0

        self.dt = 1
        self.g = 9.81
        self.f = 0.0
        self.r = 0.0
        self.A = 1

        self.h0 = np.ones((self.ny + 2, self.nx + 2), dtype=np.float32) * 60
        self.eta0 = np.zeros((self.ny + 2, self.nx + 2), dtype=np.float32)
        self.u0 = np.zeros((self.ny + 2, self.nx + 1 + 2), dtype=np.float32)
        self.v0 = np.zeros((self.ny + 1 + 2, self.nx + 2), dtype=np.float32)

        self.ghosts = [1, 1, 1, 1]  # north, east, south, west
        self.etaRange = [-1, -1, 1, 1]
        self.uRange = [-1, -2, 1, 2]
        self.vRange = [-2, -1, 2, 1]
        self.refEtaRange = self.etaRange
        self.refURange = self.uRange
        self.refVRange = self.vRange
        self.boundaryConditions = None

        self.T = 50.0

        self.sim = None
Esempio n. 12
0
    def setUp(self):
        self.gpu_ctx = Common.CUDAContext()

        self.nx = 50
        self.ny = 70

        self.dx = 200.0
        self.dy = 200.0

        self.dt = 1
        self.g = 9.81
        self.f = 0.0
        self.r = 0.0

        self.h0 = None  #np.ones((self.ny, self.nx), dtype=np.float32) * 60;
        self.eta0 = None  # np.zeros((self.ny, self.nx), dtype=np.float32);
        self.u0 = None  # np.zeros((self.ny, self.nx+1), dtype=np.float32);
        self.v0 = None  # np.zeros((self.ny+1, self.nx), dtype=np.float32);

        self.T = 50.0

        self.boundaryConditions = None  #Common.BoundaryConditions()
        self.ghosts = [1, 1, 1, 1]
        self.arrayRange = None

        self.sim = None
Esempio n. 13
0
def generateInitialConditions(sim_args, water_depth):
    from SWESimulators import CDKLM16, Common
    assert (MPI.COMM_WORLD.rank == 0)

    dataShape = (sim_args['ny'] + 4, sim_args['nx'] + 4)
    dataShapeHi = (sim_args['ny'] + 5, sim_args['nx'] + 5)

    sim_ic = {
        'H': np.ones(dataShapeHi, dtype=np.float32) * water_depth,
        'eta0': np.zeros(dataShape, dtype=np.float32),
        'hu0': np.zeros(dataShape, dtype=np.float32),
        'hv0': np.zeros(dataShape, dtype=np.float32)
    }

    #Very inefficient way of creating perturbed initial state, but works
    cuda_ctx = Common.CUDAContext()
    sim = CDKLM16.CDKLM16(cuda_ctx, **sim_args, **sim_ic)
    sim.perturbState(q0_scale=100)  # Create a random initial state
    sim_ic['eta0'], sim_ic['hu0'], sim_ic['hv0'] = sim.download(
        interior_domain_only=False)
    sim_ic['H'] = sim.downloadBathymetry()[0]
    sim = None
    gc.collect()

    return sim_ic
Esempio n. 14
0
    def setUp(self):
        self.gpu_ctx = Common.CUDAContext()

        self.sim = None
        self.file_sim = None
        
        self.printall = True
Esempio n. 15
0
    def setUp(self):
        self.nx = 1
        self.ny = 1
        self.dx = 1.0
        self.dy = 1.0
        self.dt = 1.0

        self.numParticles = 3
        self.observationVariance = 0.5
        self.boundaryCondition = Common.BoundaryConditions(2, 2, 2, 2)
        self.smallParticleSet = None
        # to be initialized by child class with above values

        # create_small_particle_set:
        self.cl_ctx = None

        self.smallPositionSetHost = np.array([[0.9, 0.9], [0.9, 0.1],
                                              [0.1, 0.9], [0.1, 0.1]])

        self.resampleNumParticles = 6
        self.resamplingParticleArray = np.zeros((7, 2))
        self.resamplingObservationVariance = 0.1
        for i in range(2):
            self.resamplingParticleArray[3 * i + 0, :] = [0.25, 0.35 + i * 0.3]
            self.resamplingParticleArray[3 * i + 1, :] = [0.4, 0.35 + i * 0.3]
            self.resamplingParticleArray[3 * i + 2, :] = [0.65, 0.35 + i * 0.3]
        self.resamplingParticleArray[6, :] = [0.25, 0.5]
        self.resamplingParticleSet = None
        # to be initialized by child class wit resampleNumParticles only.

        self.resamplingVar = 1e-8
Esempio n. 16
0
    def setUp(self):
        self.gpu_ctx = Common.CUDAContext()
        
        self.nx = 50
        self.ny = 70
        
        self.dx = 200.0
        self.dy = 200.0
        
        self.dt = 0.95
        self.g = 9.81
        self.f = 0.0
        self.r = 0.0
        self.A = 1
        
        #self.h0 = np.ones((self.ny+2, self.nx+2), dtype=np.float32) * 60;
        self.waterHeight = 60
        self.eta0 = None
        self.u0 = None
        self.v0 = None
        self.Hi = None
        self.Bi = None

        self.ghosts = [2,2,2,2] # north, east, south, west
        self.validDomain = np.array([2,2,2,2])
        self.refRange = [-2, -2, 2, 2]
        self.dataRange = self.refRange
        self.boundaryConditions = None

        self.T = 50.0
        self.sim = None
Esempio n. 17
0
    def __init__(self, numDrifters, observation_variance=0.1, \
                 boundaryConditions=Common.BoundaryConditions(), \
                 domain_size_x=1.0, domain_size_y=1.0):
        """
        Creates a GlobalParticles object for drift trajectory ensemble.

        numDrifters: number of drifers in the collection, not included the observation
        observation_variance: uncertainty of observation position
        boundaryConditions: BoundaryConditions object, relevant during re-initialization of particles.    
        """

        self.numDrifters = numDrifters

        # Observation index is the last particle
        self.obs_index = self.numDrifters
        self.observation_variance = observation_variance

        self.positions = None  # Needs to be allocated in the child class
        # Should represent all particles plus observation

        self.domain_size_x = domain_size_x
        self.domain_size_y = domain_size_y

        # Boundary conditions are read from a BoundaryConditions object
        self.boundaryConditions = boundaryConditions
Esempio n. 18
0
    def cuda_context_handler(self, line):
        args = magic_arguments.parse_argstring(self.cuda_context_handler, line)
        self.logger = logging.getLogger(__name__)

        self.logger.info("Registering %s in user workspace", args.name)

        if args.name in self.shell.user_ns.keys():
            self.logger.debug("Context already registered! Ignoring")
            return
        else:
            self.logger.debug("Creating context")
            use_cache = False if args.no_cache else True
            self.shell.user_ns[args.name] = Common.CUDAContext(
                blocking=args.blocking, use_cache=use_cache)

        # this function will be called on exceptions in any cell
        def custom_exc(shell, etype, evalue, tb, tb_offset=None):
            self.logger.exception(
                "Exception caught: Resetting to CUDA context %s", args.name)
            while (cuda.Context.get_current() != None):
                context = cuda.Context.get_current()
                self.logger.info("Popping <%s>", str(context.handle))
                cuda.Context.pop()

            if args.name in self.shell.user_ns.keys():
                self.logger.info(
                    "Pushing <%s>",
                    str(self.shell.user_ns[args.name].cuda_context.handle))
                self.shell.user_ns[args.name].cuda_context.push()
            else:
                self.logger.error(
                    "No CUDA context called %s found (something is wrong)",
                    args.name)
                self.logger.error("CUDA will not work now")

            self.logger.debug(
                "=================================================================="
            )

            # still show the error within the notebook, don't just swallow it
            shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)

        # this registers a custom exception handler for the whole current notebook
        get_ipython().set_custom_exc((Exception, ), custom_exc)

        # Handle CUDA context when exiting python
        import atexit

        def exitfunc():
            self.logger.info("Exitfunc: Resetting CUDA context stack")
            while (cuda.Context.get_current() != None):
                context = cuda.Context.get_current()
                self.logger.info("`-> Popping <%s>", str(context.handle))
                cuda.Context.pop()
            self.logger.debug(
                "=================================================================="
            )

        atexit.register(exitfunc)
Esempio n. 19
0
 def setBoundaryConditions(self, bcSettings=1):
     if (bcSettings == 1):
         self.boundaryConditions = Common.BoundaryConditions()
         #self.ghosts = [0,0,0,0] # north, east, south, west
         self.arrayRange = [None, None, 0, 0]
     elif (bcSettings == 2):
         self.boundaryConditions = Common.BoundaryConditions(2, 2, 2, 2)
         #self.ghosts = [1,1,0,0] # Both periodic
         self.arrayRange = [-1, -1, 0, 0]
     elif bcSettings == 3:
         self.boundaryConditions = Common.BoundaryConditions(2, 1, 2, 1)
         #self.ghosts = [1,0,0,0] # periodic north-south
         self.arrayRange = [-1, None, 0, 0]
     else:
         self.boundaryConditions = Common.BoundaryConditions(1, 2, 1, 2)
         #self.ghosts = [0,1,0,0] # periodic east-west
         self.arrayRange = [None, -1, 0, 0]
Esempio n. 20
0
    def setUp(self):
        self.sim = None
        self.ensemble = None
        self.iewpf = None

        self.gpu_ctx = Common.CUDAContext()

        self.setUpAndStartEnsemble()
Esempio n. 21
0
    def setGridInfo(self,
                    nx,
                    ny,
                    dx,
                    dy,
                    dt,
                    boundaryConditions=Common.BoundaryConditions(),
                    eta=None,
                    hu=None,
                    hv=None,
                    H=None):
        self.nx = nx
        self.ny = ny
        self.dx = dx
        self.dy = dy
        self.dt = dt

        # Default values for now:
        self.initialization_variance = 10 * dx
        self.midPoint = 0.5 * np.array([self.nx * self.dx, self.ny * self.dy])
        self.initialization_cov = np.eye(2) * self.initialization_variance

        self.boundaryConditions = boundaryConditions

        assert (self.simType == 'CDKLM16'
                ), 'CDKLM16 is currently the only supported scheme'
        #if self.simType == 'CDKLM16':
        self.ghostCells = np.array([2, 2, 2, 2])
        if self.boundaryConditions.isSponge():
            sponge = self.boundaryConditions.getSponge()
            for i in range(4):
                if sponge[i] > 0:
                    self.ghostCells[i] = sponge[i]
        dataShape = (ny + self.ghostCells[0] + self.ghostCells[2],
                     nx + self.ghostCells[1] + self.ghostCells[3])

        self.base_eta = eta
        self.base_hu = hu
        self.base_hv = hv
        self.base_H = H

        # Create base initial data:
        if self.base_eta is None:
            self.base_eta = np.zeros(dataShape, dtype=np.float32, order='C')
        if self.base_hu is None:
            self.base_hu = np.zeros(dataShape, dtype=np.float32, order='C')
        if self.base_hv is None:
            self.base_hv = np.zeros(dataShape, dtype=np.float32, order='C')

        # Bathymetry:
        if self.base_H is None:
            waterDepth = 10
            self.base_H = np.ones((dataShape[0] + 1, dataShape[1] + 1),
                                  dtype=np.float32,
                                  order='C') * waterDepth

        self.setParameters()
Esempio n. 22
0
 def setup_mpi(self, line):
     args = magic_arguments.parse_argstring(self.setup_mpi, line)
     logger = logging.getLogger('SWESimulators')
     if args.name in self.shell.user_ns.keys():
         logger.warning("MPI alreay set up, resetting")
         self.shell.user_ns[args.name].shutdown()
         self.shell.user_ns[args.name] = None
         gc.collect()
     self.shell.user_ns[args.name] = Common.IPEngine(args.num_engines)
Esempio n. 23
0
    def test_collection_mean(self):
        self.set_positions_small_set()
        periodicMean = [1-0.1/3, 1-0.1/3]
        nonPeriodicMean = [(0.9 + 0.9 + 0.1)/3, (0.9 + 0.9 + 0.1)/3]
        semiPeriodicMean = [nonPeriodicMean[0], periodicMean[1]]
        
        assertListAlmostEqual(self, self.smallDrifterSet.getCollectionMean().tolist(),
                              periodicMean, 6,
                              'periodic mean')

        self.smallDrifterSet.setBoundaryConditions(Common.BoundaryConditions(1,1,1,1))
        assertListAlmostEqual(self, self.smallDrifterSet.getCollectionMean().tolist(),
                              nonPeriodicMean, 6,
                              'non-periodic mean')

        self.smallDrifterSet.setBoundaryConditions(Common.BoundaryConditions(2,1,2,1))
        assertListAlmostEqual(self, self.smallDrifterSet.getCollectionMean().tolist(),
                              semiPeriodicMean, 6,
                              'north-south-periodic mean')
Esempio n. 24
0
def animateWind(wind_source, create_movie=True):
    time = wind_source.t
    
    max_stress = max(np.max(np.abs(wind_source.X)), np.max(np.abs(wind_source.Y)))
    wind_stress = np.sqrt(wind_source.X**2, wind_source.Y**2)

    fig = plt.figure(figsize=(12,3))
    
    ax = [None]*3
    sc = [None]*3
    
    ax[0] = plt.subplot(1,3,1)
    plt.title("Wind stress (total)")
    sc[0] = plt.imshow(wind_stress[0], origin='lower', vmin=0, cmap='Reds')
    plt.colorbar(shrink=0.6)

    ax[1] = plt.subplot(1,3,2)
    plt.title("Wind stress u")
    sc[1] = plt.imshow(wind_source.X[0], origin='lower', cmap='bwr', vmin=-max_stress, vmax=max_stress)
    plt.colorbar(shrink=0.6)

    ax[2] = plt.subplot(1,3,3)
    plt.title("Wind stress v")
    sc[2] = plt.imshow(wind_source.Y[0], origin='lower', cmap='bwr', vmin=-max_stress, vmax=max_stress)
    plt.colorbar(shrink=0.6)
    
    progress = Common.ProgressPrinter(5)
    pp = display(progress.getPrintString(0),display_id=True)
    

    
    #Helper function which simulates and plots the solution
    def animate(i):
        fig.suptitle("Time = {:04.0f} h".format(time[i]/3600), fontsize=18)
        
        fig.sca(ax[0])
        sc[0].set_data(wind_stress[i])
        
        fig.sca(ax[1])
        sc[1].set_data(wind_source.X[i])
        
        fig.sca(ax[2])
        sc[2].set_data(wind_source.Y[i])
        
        pp.update(progress.getPrintString(i / (len(time)-1)))

    #Matplotlib for creating an animation
    if (create_movie):
        anim = animation.FuncAnimation(fig, animate, range(len(time)), interval=250)
        plt.close(fig)
        return anim
    else:
        pass
Esempio n. 25
0
    def _setGridInfo(self,
                     nx,
                     ny,
                     dx,
                     dy,
                     dt,
                     boundaryConditions=Common.BoundaryConditions(),
                     eta=None,
                     hu=None,
                     hv=None,
                     H=None):
        """
        Declaring grid-related member variables
        """
        self.nx = nx
        self.ny = ny
        self.dx = dx
        self.dy = dy
        self.dt = dt

        self.boundaryConditions = boundaryConditions

        assert (self.simType == 'CDKLM16'
                ), 'CDKLM16 is currently the only supported scheme'
        self.ghostCells = np.array([2, 2, 2, 2])
        if self.boundaryConditions.isSponge():
            sponge = self.boundaryConditions.getSponge()
            for i in range(4):
                if sponge[i] > 0:
                    self.ghostCells[i] = sponge[i]
        dataShape = (ny + self.ghostCells[0] + self.ghostCells[2],
                     nx + self.ghostCells[1] + self.ghostCells[3])

        self.base_eta = eta
        self.base_hu = hu
        self.base_hv = hv
        self.base_H = H

        # Create base initial data:
        if self.base_eta is None:
            self.base_eta = np.zeros(dataShape, dtype=np.float32, order='C')
        if self.base_hu is None:
            self.base_hu = np.zeros(dataShape, dtype=np.float32, order='C')
        if self.base_hv is None:
            self.base_hv = np.zeros(dataShape, dtype=np.float32, order='C')

        # Bathymetry:
        if self.base_H is None:
            waterDepth = 10
            self.base_H = np.ones((dataShape[0] + 1, dataShape[1] + 1),
                                  dtype=np.float32,
                                  order='C') * waterDepth
Esempio n. 26
0
    def test_copy_buffer(self):
        clarray2 = Common.CUDAArray2D(self.gpu_stream, \
                                      self.nx, self.ny, self.nx_halo, self.ny_halo, \
                                      self.buf3)

        host_data_pre_copy = self.cudaarray.download(self.gpu_stream)
        self.assertEqual(host_data_pre_copy.tolist(), self.buf1.tolist())

        self.cudaarray.copyBuffer(self.gpu_stream, clarray2)
        host_data_post_copy = self.cudaarray.download(self.gpu_stream)
        self.assertEqual(host_data_post_copy.tolist(), self.buf3.tolist())

        self.tests_failed = False
Esempio n. 27
0
    def test_copy_buffer(self):
        clarray2 = Common.OpenCLArray2D(self.cl_ctx, \
                                        self.nx, self.ny, self.nx_halo, self.ny_halo, \
                                        self.buf3)

        host_data_pre_copy = self.clarray.download(self.cl_queue)
        self.assertEqual(host_data_pre_copy.tolist(), self.buf1.tolist())

        self.clarray.copyBuffer(self.cl_queue, clarray2)
        host_data_post_copy = self.clarray.download(self.cl_queue)
        self.assertEqual(host_data_post_copy.tolist(), self.buf3.tolist())

        self.tests_failed = False
Esempio n. 28
0
 def create_noise(self):
     n, e, s, w = 1, 1, 1, 1
     if self.periodicNS:
         n, s = 2, 2
     if self.periodicEW:
         e, w = 2, 2
     self.noise = OceanStateNoise(self.gpu_ctx,
                                  self.gpu_stream,
                                  self.nx,
                                  self.ny,
                                  self.dx,
                                  self.dy,
                                  Common.BoundaryConditions(n, e, s, w),
                                  staggered=self.staggered)
Esempio n. 29
0
    def setUp(self):

        #Set which CL device to use, and disable kernel caching
        self.gpu_ctx = Common.CUDAContext()

        # Make some host data which we can play with
        self.nx = 3
        self.ny = 5
        self.nx_halo = 1
        self.ny_halo = 2
        self.dataShape = (self.ny + 2 * self.ny_halo,
                          self.nx + 2 * self.nx_halo)

        self.buf1 = np.zeros(self.dataShape, dtype=np.float32, order='C')
        self.dbuf1 = np.zeros(self.dataShape)
        self.buf3 = np.zeros(self.dataShape, dtype=np.float32, order='C')
        self.dbuf3 = np.zeros(self.dataShape)
        for j in range(self.dataShape[0]):
            for i in range(self.dataShape[1]):
                self.buf1[j, i] = i * 100 + j
                self.dbuf1[j, i] = self.buf1[j, i]
                self.buf3[j, i] = j * 1000 - i
                self.dbuf3[j, i] = self.buf3[j, i]

        self.explicit_free = False

        self.device_name = self.gpu_ctx.cuda_device.name()
        self.gpu_stream = cuda.Stream()

        self.tests_failed = True

        self.cudaarray = Common.CUDAArray2D(self.gpu_stream, \
                                            self.nx, self.ny, \
                                            self.nx_halo, self.ny_halo, \
                                            self.buf1)

        self.double_cudaarray = None
Esempio n. 30
0
    def setUp(self):
        self.gpu_ctx = Common.CUDAContext()
        self.gpu_stream = cuda.Stream()

        self.nx = 30
        self.ny = 40
        self.dx = 7.0
        self.dy = 7.0

        self.f = 0.02
        self.g = 9.81
        self.beta = 0.0

        self.noise = None

        self.ghost_cells_x = 2
        self.ghost_cells_y = 2
        self.datashape = (self.ny + 2 * self.ghost_cells_y,
                          self.nx + 2 * self.ghost_cells_x)

        self.cutoff = 2
        self.nx_nonPeriodic = self.nx + 2 * (2 + self.cutoff)
        self.ny_nonPeriodic = self.ny + 2 * (2 + self.cutoff)

        # Standard setup is non-staggered, periodic
        self.staggered = False
        self.periodicNS = True
        self.periodicEW = True

        # Total number of threads should be: 16, 32, 48, 64
        # Corresponding to the number of blocks: 1, 2, 3, 4
        self.glob_size_x = 3
        self.glob_size_y = 3
        self.glob_size_x_nonperiodic = 3
        self.glob_size_y_nonperiodic = 3
        self.glob_size_random_x = 1
        self.glob_size_random_x_nonperiodic = 2

        self.large_nx = 400
        self.large_ny = 400
        self.large_noise = None

        self.floatMax = 2147483648.0

        self.eta = None
        self.hu = None
        self.hv = None
        self.H = None