def test_from_netcdf_memory_containment(mode, time_periodic, field_chunksize, with_GC): if field_chunksize == 'auto': dask.config.set({'array.chunk-size': '2MiB'}) else: dask.config.set({'array.chunk-size': '128MiB'}) class PerformanceLog(): samples = [] memory_steps = [] _iter = 0 def advance(self): process = psutil.Process(os.getpid()) self.memory_steps.append(process.memory_info().rss) self.samples.append(self._iter) self._iter += 1 def perIterGC(): gc.collect() def periodicBoundaryConditions(particle, fieldset, time): while particle.lon > 180.: particle.lon -= 360. while particle.lon < -180.: particle.lon += 360. while particle.lat > 90.: particle.lat -= 180. while particle.lat < -90.: particle.lat += 180. process = psutil.Process(os.getpid()) mem_0 = process.memory_info().rss fnameU = path.join(path.dirname(__file__), 'test_data', 'perlinfieldsU.nc') fnameV = path.join(path.dirname(__file__), 'test_data', 'perlinfieldsV.nc') ufiles = [ fnameU, ] * 4 vfiles = [ fnameV, ] * 4 timestamps = np.arange(0, 4, 1) * 86400.0 timestamps = np.expand_dims(timestamps, 1) files = {'U': ufiles, 'V': vfiles} variables = {'U': 'vozocrtx', 'V': 'vomecrty'} dimensions = {'lon': 'nav_lon', 'lat': 'nav_lat'} fieldset = FieldSet.from_netcdf(files, variables, dimensions, timestamps=timestamps, time_periodic=time_periodic, allow_time_extrapolation=True if time_periodic in [False, None] else False, field_chunksize=field_chunksize) perflog = PerformanceLog() postProcessFuncs = [ perflog.advance, ] if with_GC: postProcessFuncs.append(perIterGC) pset = ParticleSet(fieldset=fieldset, pclass=ptype[mode], lon=[ 0.5, ], lat=[ 0.5, ]) mem_0 = process.memory_info().rss mem_exhausted = False try: pset.execute(pset.Kernel(AdvectionRK4) + periodicBoundaryConditions, dt=delta(hours=1), runtime=delta(days=7), postIterationCallbacks=postProcessFuncs, callbackdt=delta(hours=12)) except MemoryError: mem_exhausted = True mem_steps_np = np.array(perflog.memory_steps) if with_GC: assert np.allclose(mem_steps_np[8:], perflog.memory_steps[-1], rtol=0.01) if (field_chunksize is not False or with_GC) and mode != 'scipy': assert np.alltrue((mem_steps_np - mem_0) < 4712832) # represents 4 x [U|V] * sizeof(field data) assert not mem_exhausted
default='stationary', help='Generate fieldset file with given dimensions') p.add_argument('-m', '--method', choices=('RK4', 'EE', 'RK45'), default='RK4', help='Numerical method used for advection') args = p.parse_args() filename = 'analytical_eddies' # Generate fieldset files according to chosen test setup if args.fieldset == 'stationary': fieldset = fieldset_stationary() elif args.fieldset == 'moving': fieldset = fieldset_moving() elif args.fieldset == 'decaying': fieldset = fieldset_decaying() npart = args.particles pset = ParticleSet(fieldset, pclass=ptype[args.mode], lon=np.linspace(4000, 21000, npart), lat=np.linspace(12500, 12500, npart)) if args.verbose: print("Initial particle positions:\n%s" % pset) pset.execute(kernel[args.method], dt=delta(minutes=3), runtime=delta(hours=6)) if args.verbose: print("Final particle positions:\n%s" % pset)
def test_summedfields(mode, with_W, k_sample_p, mesh): xdim = 10 ydim = 20 zdim = 4 gf = 10 # factor by which the resolution of grid1 is higher than of grid2 U1 = Field('U', 0.2 * np.ones( (zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), lon=np.linspace(0., 1., xdim * gf, dtype=np.float32), lat=np.linspace(0., 1., ydim * gf, dtype=np.float32), depth=np.linspace(0., 20., zdim * gf, dtype=np.float32), mesh=mesh) U2 = Field('U', 0.1 * np.ones((zdim, ydim, xdim), dtype=np.float32), lon=np.linspace(0., 1., xdim, dtype=np.float32), lat=np.linspace(0., 1., ydim, dtype=np.float32), depth=np.linspace(0., 20., zdim, dtype=np.float32), mesh=mesh) V1 = Field('V', np.zeros((zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), grid=U1.grid, fieldtype='V') V2 = Field('V', np.zeros((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid, fieldtype='V') fieldsetS = FieldSet(U1 + U2, V1 + V2) conv = 1852 * 60 if mesh == 'spherical' else 1. assert np.allclose(fieldsetS.U[0, 0, 0, 0] * conv, 0.3) P1 = Field('P', 30 * np.ones( (zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), grid=U1.grid) P2 = Field('P', 20 * np.ones((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid) P3 = Field('P', 10 * np.ones((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid) P4 = Field('P', 0 * np.ones((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid) fieldsetS.add_field((P1 + P4) + (P2 + P3), name='P') assert np.allclose(fieldsetS.P[0, 0, 0, 0], 60) if with_W: W1 = Field('W', 2 * np.ones( (zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), grid=U1.grid) W2 = Field('W', np.ones((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid) fieldsetS.add_field(W1 + W2, name='W') pset = ParticleSet(fieldsetS, pclass=pclass(mode), lon=[0], lat=[0.9]) pset.execute(AdvectionRK4_3D + pset.Kernel(k_sample_p), runtime=2, dt=1) assert np.isclose(pset.depth[0], 6) else: pset = ParticleSet(fieldsetS, pclass=pclass(mode), lon=[0], lat=[0.9]) pset.execute(AdvectionRK4 + pset.Kernel(k_sample_p), runtime=2, dt=1) assert np.isclose(pset.p[0], 60) assert np.isclose(pset.lon[0] * conv, 0.6, atol=1e-3) assert np.isclose(pset.lat[0], 0.9) assert np.allclose(fieldsetS.UV[0][0, 0, 0, 0], [.2 / conv, 0])
def run_hycom_cfsr_subset_monthly_release( output_dir, output_name, time0, lon0, lat0, start_date, end_date, windage=0.03, input_dir_hycom=get_dir('hycom_input'), input_dir_cfsr=get_dir('cfsr_input'), indices_hycom=_get_io_indices_from_netcdf(), indices_cfsr=_get_io_indices_from_netcdf( input_path=get_dir('cfsr_indices_input')), kh=10., interp_method='linear'): # get paths ncfiles_hycom = get_daily_ncfiles_in_time_range(input_dir_hycom, start_date, end_date) ncfiles_cfsr = get_daily_ncfiles_in_time_range(input_dir_cfsr, start_date, end_date) output_path = output_dir + output_name # create fieldset filenames_hycom = [input_dir_hycom + ncfile for ncfile in ncfiles_hycom] filenames_cfsr = [input_dir_cfsr + ncfile for ncfile in ncfiles_cfsr] variables_hycom = {'U': 'u', 'V': 'v'} variables_cfsr = {'U': 'u', 'V': 'v'} dimensions_hycom = {'lat': 'lat', 'lon': 'lon', 'time': 'time'} dimensions_cfsr = {'lat': 'lat', 'lon': 'lon', 'time': 'time'} fset_hycom = FieldSet.from_netcdf(filenames_hycom, variables_hycom, dimensions_hycom, indices=indices_hycom) fset_cfsr = FieldSet.from_netcdf(filenames_cfsr, variables_cfsr, dimensions_cfsr, indices=indices_cfsr) fset_cfsr.U.set_scaling_factor(windage) fset_cfsr.V.set_scaling_factor(windage) fset = FieldSet(U=fset_hycom.U + fset_cfsr.U, V=fset_hycom.V + fset_cfsr.V) # add constant horizontal diffusivity (zero on land) lm = LandMask.read_from_netcdf() kh2D = kh * np.ones(lm.mask.shape) kh2D[lm.mask.astype('bool')] = 0.0 # diffusion zero on land kh2D_subset = kh2D[indices_hycom['lat'], :][:, indices_hycom['lon']] fset.add_field( Field('Kh_zonal', data=kh2D_subset, lon=fset_hycom.U.grid.lon, lat=fset_hycom.U.grid.lat, mesh='spherical', interp_method=interp_method)) fset.add_field( Field('Kh_meridional', data=kh2D_subset, lon=fset_hycom.U.grid.lon, lat=fset_hycom.U.grid.lat, mesh='spherical', interp_method=interp_method)) # monthly release pset = ParticleSet(fieldset=fset, pclass=JITParticle, lon=lon0, lat=lat0, time=time0) # execute run_time = timedelta(days=(end_date - start_date).days) dt = timedelta(hours=1) output_interval = 24 kernel = pset.Kernel(AdvectionRK4) + pset.Kernel(DiffusionUniformKh) output_file = pset.ParticleFile(name=output_path, outputdt=dt * output_interval) pset.execute(kernel, runtime=run_time, dt=dt, output_file=output_file, verbose_progress=True, recovery={ErrorCode.ErrorOutOfBounds: delete_particle})
def test_globcurrent_dt0(mode, use_xarray): fieldset = set_globcurrent_fieldset(use_xarray=use_xarray) pset = ParticleSet(fieldset, pclass=ptype[mode], lon=[25], lat=[-35]) pset.execute(AdvectionRK4, dt=0.)
def test_3d_2dfield_sampling(mode): data_path = path.join(path.dirname(__file__), 'NemoNorthSeaORCA025-N006_data/') ufiles = sorted(glob(data_path + 'ORCA*U.nc')) vfiles = sorted(glob(data_path + 'ORCA*V.nc')) mesh_mask = data_path + 'coordinates.nc' filenames = { 'U': { 'lon': mesh_mask, 'lat': mesh_mask, 'data': ufiles }, 'V': { 'lon': mesh_mask, 'lat': mesh_mask, 'data': vfiles }, 'nav_lon': { 'lon': mesh_mask, 'lat': mesh_mask, 'data': [ ufiles[0], ] } } variables = {'U': 'uo', 'V': 'vo', 'nav_lon': 'nav_lon'} dimensions = { 'U': { 'lon': 'glamf', 'lat': 'gphif', 'time': 'time_counter' }, 'V': { 'lon': 'glamf', 'lat': 'gphif', 'time': 'time_counter' }, 'nav_lon': { 'lon': 'glamf', 'lat': 'gphif' } } fieldset = FieldSet.from_nemo(filenames, variables, dimensions, chunksize=False) fieldset.nav_lon.data = np.ones(fieldset.nav_lon.data.shape, dtype=np.float32) fieldset.add_field( Field('rectilinear_2D', np.ones((2, 2)), lon=np.array([-10, 20]), lat=np.array([40, 80]), chunksize=False)) class MyParticle(ptype[mode]): sample_var_curvilinear = Variable('sample_var_curvilinear') sample_var_rectilinear = Variable('sample_var_rectilinear') pset = ParticleSet(fieldset, pclass=MyParticle, lon=2.5, lat=52) def Sample2D(particle, fieldset, time): particle.sample_var_curvilinear += fieldset.nav_lon[time, particle.depth, particle.lat, particle.lon] particle.sample_var_rectilinear += fieldset.rectilinear_2D[ time, particle.depth, particle.lat, particle.lon] runtime, dt = 86400 * 4, 6 * 3600 pset.execute(pset.Kernel(AdvectionRK4) + Sample2D, runtime=runtime, dt=dt) assert pset.sample_var_rectilinear == runtime / dt assert pset.sample_var_curvilinear == runtime / dt
def sequential(start_date, end_date, config, name='', winds=True, diffusion=True, unbeaching=True, restart_file=""): # years = config[WorldLitter.years] years = np.arange(start_date.year, end_date.year + 1) base_folder = config[GlobalModel.base_folder] release_loc_folder = config[GlobalModel.loc_folder] output_file = join(config[GlobalModel.output_folder], name) unbeach_file = config[GlobalModel.unbeach_file] lat_files = config[GlobalModel.lat_files] lon_files = config[GlobalModel.lon_files] dt = config[GlobalModel.dt] kh = 1 repeat_release = config[GlobalModel.repeat_release] run_time = timedelta(seconds=(end_date - start_date).total_seconds()) file_names = read_files(base_folder, years, wind=winds, start_date=start_date, end_date=end_date) if len(file_names) == 0: raise Exception("ERROR: We couldn't read any file!") print("Reading initial positions.....") lat0 = functools.reduce(lambda a, b: np.concatenate((a, b), axis=0), [ np.genfromtxt(join(release_loc_folder, x), delimiter='') for x in lat_files ]) lon0 = functools.reduce(lambda a, b: np.concatenate((a, b), axis=0), [ np.genfromtxt(join(release_loc_folder, x), delimiter='') for x in lon_files ]) variables = {'U': 'surf_u', 'V': 'surf_v'} dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'} print("Reading netcdf files.....", flush=True) # Adding the vector fields it may be currents or currents + winds main_fieldset = FieldSet.from_netcdf(file_names, variables, dimensions, allow_time_extrapolation=True, field_chunksize=(2048, 2048)) # ------- Making syntetic diffusion coefficient U_grid = main_fieldset.U.grid lat = U_grid.lat lon = U_grid.lon # Getting proporcional size by degree if diffusion: print("Adding diffusion .....") add_Kh(main_fieldset, lat, lon, kh) if unbeaching: print("Adding unbeaching.....") add_unbeaching_field(main_fieldset, lat, lon, unbeach_file) # ------- Adding constants for periodic halo main_fieldset.add_constant('halo_west', main_fieldset.U.grid.lon[0]) main_fieldset.add_constant('halo_east', main_fieldset.U.grid.lon[-1]) main_fieldset.add_periodic_halo(zonal=True) #create a zonal halo print("Setting up everything.....") if unbeaching: particle_class = LitterParticle else: particle_class = JITParticle if restart_file != '': print(F"Using restart file {restart_file}") pset = ParticleSet.from_particlefile(fieldset=main_fieldset, pclass=particle_class, filename=restart_file, repeatdt=repeat_release) else: pset = ParticleSet(fieldset=main_fieldset, pclass=particle_class, lon=lon0, lat=lat0, repeatdt=repeat_release) out_parc_file = pset.ParticleFile(name=output_file, outputdt=config[GlobalModel.output_freq]) t = time.time() print(F"Adding kernels...") if unbeaching: kernels = pset.Kernel(AdvectionRK4Beached) else: kernels = pset.Kernel(AdvectionRK4) if unbeaching: kernels += pset.Kernel(BeachTesting_2D) kernels += pset.Kernel(UnBeaching) if diffusion: kernels += pset.Kernel(BrownianMotion2DUnbeaching) kernels += pset.Kernel(BeachTesting_2D) else: if diffusion: kernels += pset.Kernel(BrownianMotion2D) kernels += pset.Kernel(periodicBC) print(F"Running with {pset.size} number of particles for {run_time}", flush=True) pset.execute(kernels, runtime=run_time, dt=dt, output_file=out_parc_file, recovery={ErrorCode.ErrorOutOfBounds: outOfBounds}) print(F"Done time={time.time()-t}.....") print(F"Saving output to {output_file}!!!!!") # domain = {'N': 31, 'S': 16, 'E': -76, 'W': -98} # pset.show(field=main_fieldset.U, domain=domain) # Draw current particles out_parc_file.export() # Save trajectories to file if MPI: print( F"----- Waiting for file to be saved proc {MPI.COMM_WORLD.Get_rank()} ... ---------", flush=True) MPI.COMM_WORLD.Barrier() # out_parc_file.close() # del pset # del kernels # del main_fieldset # # plotTrajectoriesFile(output_file) # Plotting trajectories # print("Forcing gc.collect") # gc.collect() print("Done!!!!!!!!!!!! YEAH BABE!!!!!!!!", flush=True)
particle.delete() def DeleteParticle(particle, fieldset, time): particle.delete() #additional features of the particles class GalapagosParticle(JITParticle): age = Variable('age', initial=0.) # set particle conditions pset = ParticleSet(fieldset=fieldset, pclass=GalapagosParticle, lon=startlon, lat=startlat, time=fU.grid.time[-1], repeatdt=delta(days=5)) outfile = pset.ParticleFile(name=fname, outputdt=delta(days=1)) pset.execute(AdvectionRK4 + pset.Kernel(Age), runtime=delta(days=365), dt=delta(hours=-1), output_file=outfile, recovery={ErrorCode.ErrorOutOfBounds: DeleteParticle}) pset.repeatdt = None pset.execute(AdvectionRK4 + pset.Kernel(Age), runtime=delta(days=300),
#create particle set #subsets of North Atlantic #lons = np.load(griddir + 'Lons' + str(pos) + '.npy') #lats = np.load(griddir + 'Lats' + str(pos) + '.npy') #whole North Atlantic lons = np.load(griddir + 'Lons_full02' + '.npy') lats = np.load(griddir + 'Lats_full02' + '.npy') times = [datetime(year=2010, month=1, day=10)] * len(lons) depths = [0.] * len(lons) #print ('Number of particles: %s' % len(lons)) pset = ParticleSet(fieldset=fieldset, pclass=VeloParticle, lon=lons, lat=lats, time=times) #pclass = VeloParticle OR JITParticle #pset.show(field=fieldset.U) kernels = pset.Kernel(AdvectionRK4) + pset.Kernel(kernel_track_velocity_2D) """Now execute the kernels for 30 days, saving data every 30 minutes""" pset.execute(kernels, runtime=timedelta(days=simdays), dt=timedelta(minutes=10), output_file=pset.ParticleFile(name=outfile, outputdt=timedelta(hours=3)), recovery={ErrorCode.ErrorOutOfBounds: DeleteParticle})
default='stationary', help='Generate grid file with given dimensions') p.add_argument('-m', '--method', choices=('RK4', 'EE', 'RK45'), default='RK4', help='Numerical method used for advection') args = p.parse_args() filename = 'analytical_eddies' # Generate grid files according to chosen test setup if args.grid == 'stationary': grid = grid_stationary() elif args.grid == 'moving': grid = grid_moving() elif args.grid == 'decaying': grid = grid_decaying() npart = args.particles pset = ParticleSet(grid, pclass=ptype[args.mode], lon=np.linspace(4000, 21000, npart, dtype=np.float32), lat=np.linspace(12500, 12500, npart, dtype=np.float32)) if args.verbose: print("Initial particle positions:\n%s" % pset) pset.execute(kernel[args.method], dt=delta(minutes=3), endtime=delta(hours=6)) if args.verbose: print("Final particle positions:\n%s" % pset)
#lats = np.load(griddir + 'Lats' + str(pos) + '.npy') #lons = [-90] #lats = [25] #for i in range(len(lons)): # print (lons[i], lats[i]) #exit(0) times = [datetime(year=2010, month=1, day=10)] * len(lons) #print(times) depths = [0.] * len(lons) #print ('Number of particles: %s' % len(lons)) pset = ParticleSet(fieldset=fieldset, pclass=VeloParticle3D, lon=lons, lat=lats, depth=depths, time=times) # time default is 0 #pset.show(field=fieldset.U) kernels = pset.Kernel(AdvectionRK4) + pset.Kernel( TrackVelocity3D) + pset.Kernel(kernel_kukulka_mixing) # #Trajectory computation #print (pset[0]) pset.execute(kernels, runtime=timedelta(days=simdays), dt=timedelta(minutes=10), output_file=pset.ParticleFile(name=outfile, outputdt=timedelta(hours=3)), recovery={ErrorCode.ErrorOutOfBounds: DeleteParticle})
def run(flow, dt, bconstant, foldername='21objects'): DistParticle.setLastID(0) filename = flow fb = 'forward' # variable to determine whether the flowfields are analysed 'forward' or 'backward' in time bconstant = bconstant corald = Dataset(foldername + '/' + filename + '.nc', 'r+') # read netcdf file with input # Extract all variables into np arrays --> in the future xarray will be used T = corald.variables['T'][:] X = corald.variables['X'][:] Y = corald.variables['Y'][:] U = corald.variables['U'][:] V = corald.variables['V'][:] corald.close() U = np.asarray(U) U = np.expand_dims(U, 2) # add a third dimension V = np.asarray(V) V = np.expand_dims(V, 2) # add a third dimension t = num2date(T[:61], units='seconds since 2000-01-01 00:00:00.0' ) # make t a datetime object t = date2num(t, units='seconds since 2000-01-01 00:00:00.0') times = t xs = X ys = np.asarray( [-1, 0, 1] ) # third dimension with length 3. 2D flow field will be inserted on the middle value to ensure the AdvectionRK4_3D works correctly depths = -Y # Y was height, but parcels expects depth u = np.zeros((61, U.shape[1], U.shape[2], U.shape[3])) u = np.concatenate((u, u, u), axis=2) # add the third dimension u[:, :, 1, :] = U[:61, :, 0, :] # add the data to the middle value of the third dimension v = np.zeros(u.shape) w = np.zeros(u.shape) w[:, :, 1, :] = -V[:61, :, 0, :] # because depth = -Y, w = -V dist = np.zeros(u.shape) distancemap = np.load(foldername + '/preprocessed/' + 'distancemap.npy') dist[:, :, 1, :] = np.asarray([distancemap] * len(u)) closest = np.zeros(u.shape) closestobject = np.load(foldername + '/preprocessed/' + 'closestobject.npy') closest[:, :, 1, :] = np.asarray([closestobject] * len(u)) border = np.zeros(u.shape) bordermap = np.load(foldername + '/preprocessed/' + 'bordermap.npy') border[:, :, 1, :] = np.asarray([bordermap] * len(u)) data = {'U': u, 'V': v, 'W': w, 'B': border, 'C': closest, 'D': dist} dimensions = {'lon': xs, 'lat': ys, 'depth': depths, 'time': times} fieldset = FieldSet.from_data(data=data, dimensions=dimensions, mesh='flat') fieldset.B.interp_method = 'nearest' fieldset.C.interp_method = 'nearest' fieldset.add_constant('dx', X[1] - X[0]) fieldset.add_constant('beaching', bconstant) fieldset.add_constant('x0', xs[0]) fieldset.add_constant('y0', ys[0]) fieldset.add_constant('z0', depths[0]) lons, ds = np.meshgrid( xs, depths[:]) # meshgrid at all gridpoints in the flow data um = np.ma.masked_invalid( u[0, :, 1, :] ) # retrieve mask from flowfield to take out points over coral objects lons = np.ma.masked_array(lons, mask=um.mask[:, :]) # mask points in meshgrid lons = np.ma.filled(lons, -999) lons = lons.flatten() ds = np.ma.masked_array(ds, mask=um.mask[:, :]) # mask points in meshgrid ds = np.ma.filled(ds, -999) ds = ds.flatten() outputdt = timedelta(seconds=0.1) # timesteps to create output at dt = timedelta(seconds=dt) # timesteps to calculate particle trajectories runtime = timedelta(seconds=60) # total time to execute the particleset lats = np.asarray( [0] * len(lons) ) # all particles must start and stay on the middle value of the extra dimension inittime = np.asarray( [0] * len(lons)) # default time to start the particles is zero if fb == 'backward': # change timestep and start time when in 'backward' mode dt = dt * -1 inittime = np.asarray([runtime.seconds] * len(lons)) pset = ParticleSet(fieldset=fieldset, pclass=DistParticle, lon=lons, lat=lats, depth=ds, time=inittime) n1_part = pset.size k_removeNaNs = pset.Kernel(removeNaNs) k_sample = pset.Kernel( Samples) # Casting the SampleP function to a kernel. pset.execute(k_removeNaNs + k_sample, runtime=timedelta(seconds=0)) n2_part = pset.size k_dist = pset.Kernel( FinalDistance) # Casting the FinalDistance function to a kernel. k_bound = pset.Kernel( boundary_advectionRK4_3D ) # Casting the Boundary_Advection function to a kernel. output_file = pset.ParticleFile( name=foldername + '/pfiles/B' + str(fieldset.beaching) + '-' + flow + '-' + str(abs(dt.total_seconds()))[2:] + '-' + fb, outputdt=outputdt) stime = ostime.time() pset.execute(k_bound + k_dist + k_sample, runtime=runtime, dt=dt, recovery={ErrorCode.ErrorOutOfBounds: deleteparticle}, output_file=output_file) etime = ostime.time() output_file.add_metadata('outputdt', str(outputdt.total_seconds()) + ' in seconds') output_file.add_metadata('runtime', str(runtime.total_seconds()) + ' in seconds') output_file.add_metadata('dt', str(dt.total_seconds()) + ' in seconds') output_file.add_metadata('dx', float(np.abs(X[1] - X[0]))) output_file.add_metadata('executiontime', str(etime - stime) + ' in seconds') output_file.add_metadata('beaching_strategy', fieldset.beaching) output_file.close() n3_part = pset.size print( 'Amount of particles at initialisation, 0th timestep and after execution respectively:' + str(n1_part) + ', ' + str(n2_part) + ', ' + str(n3_part))
mesh='spherical') kh_zonal = Field('Kh_zonal', kh * np.ones((len(lat), len(lon)), dtype=np.float32), lon=lon, lat=lat, allow_time_extrapolation=False, fieldtype='Kh_zonal', mesh='spherical') field_set = FieldSet(Uflow, Vflow) print("Initalizing objects........") # Initialize Particle set object # pset = ParticleSet(fieldset=field_set, pclass=JITParticle, pset = ParticleSet(fieldset=field_set, pclass=ScipyParticle, lon=np.linspace(20, 60, npart), lat=np.linspace(20, 60, npart)) out_parc_file = pset.ParticleFile(name=output_file, outputdt=1) # Draw initial particles print(F"Running with {pset.size} number of particles") pset.execute(AdvectionRK4 + pset.Kernel(BrownianMotion2D_OZ), runtime=20, dt=1, output_file=out_parc_file) print("Plotting saved output........") out_parc_file.export() # Save trajectories to file # plotTrajectoriesFile(output_file, mode='2d') # Plotting trajectories # plotTrajectoriesFile(output_file, mode='3d') # Plotting trajectories
def test_sampling_out_of_bounds_time(mode, allow_time_extrapolation, k_sample_p, xdim=10, ydim=10, tdim=10): dimensions = { 'lon': np.linspace(0., 1., xdim, dtype=np.float32), 'lat': np.linspace(0., 1., ydim, dtype=np.float32), 'time': np.linspace(0., 1., tdim, dtype=np.float64) } data = { 'U': np.zeros((xdim, ydim, tdim), dtype=np.float32), 'V': np.zeros((xdim, ydim, tdim), dtype=np.float32), 'P': np.ones((xdim, ydim, 1), dtype=np.float32) * dimensions['time'] } fieldset = FieldSet.from_data( data, dimensions, mesh='flat', allow_time_extrapolation=allow_time_extrapolation, transpose=True) pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0.5], lat=[0.5], time=-1.0) if allow_time_extrapolation: pset.execute(k_sample_p, endtime=-0.9, dt=0.1) assert np.allclose(np.array([p.p for p in pset]), 0.0, rtol=1e-5) else: with pytest.raises(RuntimeError): pset.execute(k_sample_p, endtime=-0.9, dt=0.1) pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0.5], lat=[0.5], time=0) pset.execute(k_sample_p, runtime=0.1, dt=0.1) assert np.allclose(np.array([p.p for p in pset]), 0.0, rtol=1e-5) pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0.5], lat=[0.5], time=0.5) pset.execute(k_sample_p, runtime=0.1, dt=0.1) assert np.allclose(np.array([p.p for p in pset]), 0.5, rtol=1e-5) pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0.5], lat=[0.5], time=1.0) pset.execute(k_sample_p, runtime=0.1, dt=0.1) assert np.allclose(np.array([p.p for p in pset]), 1.0, rtol=1e-5) pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0.5], lat=[0.5], time=2.0) if allow_time_extrapolation: pset.execute(k_sample_p, runtime=0.1, dt=0.1) assert np.allclose(np.array([p.p for p in pset]), 1.0, rtol=1e-5) else: with pytest.raises(RuntimeError): pset.execute(k_sample_p, runtime=0.1, dt=0.1)
def compute_globcurrent_particle_advection(field_set, mode, lonp, latp): pset = ParticleSet(field_set, pclass=ptype[mode], lon=lonp, lat=latp) pfile = ParticleFile("globcurrent_particles_chunk", pset, outputdt=delta(hours=2)) pset.execute(AdvectionRK4, runtime=delta(days=1), dt=delta(minutes=5), output_file=pfile) return pset
def test_list_of_fields(mode, with_W, k_sample_p, mesh): xdim = 10 ydim = 20 zdim = 4 gf = 10 # factor by which the resolution of grid1 is higher than of grid2 U1 = Field('U1', 0.2 * np.ones( (zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), lon=np.linspace(0., 1., xdim * gf, dtype=np.float32), lat=np.linspace(0., 1., ydim * gf, dtype=np.float32), depth=np.linspace(0., 20., zdim * gf, dtype=np.float32), mesh=mesh, fieldtype='U') U2 = Field('U2', 0.1 * np.ones((zdim, ydim, xdim), dtype=np.float32), lon=np.linspace(0., 1., xdim, dtype=np.float32), lat=np.linspace(0., 1., ydim, dtype=np.float32), depth=np.linspace(0., 20., zdim, dtype=np.float32), mesh=mesh, fieldtype='U') V1 = Field('V1', np.zeros((zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), grid=U1.grid, fieldtype='V') V2 = Field('V2', np.zeros((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid, fieldtype='V') fieldset = FieldSet([U1, U2], [V1, V2]) conv = 1852 * 60 if mesh == 'spherical' else 1. assert np.allclose(fieldset.U.eval(0, 0, 0, 0) * conv, 0.3) assert np.allclose(fieldset.U[0, 0, 0, 0] * conv, 0.3) P1 = Field('P1', 30 * np.ones( (zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), grid=U1.grid) P2 = Field('P2', 20 * np.ones((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid) fieldset.add_field([P1, P2], name='P') assert np.allclose(fieldset.P[0, 0, 0, 0], 50) if with_W: W1 = Field('W1', 2 * np.ones( (zdim * gf, ydim * gf, xdim * gf), dtype=np.float32), grid=U1.grid) W2 = Field('W2', np.ones((zdim, ydim, xdim), dtype=np.float32), grid=U2.grid) fieldset.add_field([W1, W2], name='W') pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0], lat=[0.9]) pset.execute(AdvectionRK4_3D + pset.Kernel(k_sample_p), runtime=2, dt=1) assert np.isclose(pset[0].depth, 6) else: pset = ParticleSet(fieldset, pclass=pclass(mode), lon=[0], lat=[0.9]) pset.execute(AdvectionRK4 + pset.Kernel(k_sample_p), runtime=2, dt=1) assert np.isclose(pset[0].p, 50) assert np.isclose(pset[0].lon * conv, 0.6, atol=1e-3) assert np.isclose(pset[0].lat, 0.9)
def compute_ofam_particle_advection(field_set, mode, lonp, latp, depthp): pset = ParticleSet(field_set, pclass=ptype[mode], lon=lonp, lat=latp, depth=depthp) pfile = ParticleFile("ofam_particles_chunk", pset, outputdt=delta(minutes=10)) pset.execute(AdvectionRK4, runtime=delta(days=10), dt=delta(minutes=5), output_file=pfile) return pset
def test_pset_create_lon_lat(fieldset, mode, npart=100): lon = np.linspace(0, 1, npart, dtype=np.float32) lat = np.linspace(1, 0, npart, dtype=np.float32) pset = ParticleSet(fieldset, lon=lon, lat=lat, pclass=ptype[mode]) assert np.allclose([p.lon for p in pset], lon, rtol=1e-12) assert np.allclose([p.lat for p in pset], lat, rtol=1e-12)
def test_fieldset_polar_with_halo(fieldset_geometric_polar, mode): fieldset_geometric_polar.add_periodic_halo(zonal=5) pset = ParticleSet(fieldset_geometric_polar, pclass=pclass(mode), lon=0, lat=0) pset.execute(runtime=1) assert(pset[0].lon == 0.)
parcels.VectorField, parcels.NestedField, parcels.SummedField ]: continue else: if backwardSimulation: simStart = f.grid.time_full[-1] else: simStart = f.grid.time_full[0] break if backwardSimulation: # ==== backward simulation ==== # if repeatdtFlag: pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=np.random.rand(96, 1) * 1e-5, lat=np.random.rand(96, 1) * 1e-5, time=simStart, repeatdt=delta(hours=1)) else: pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=np.random.rand(96, 1) * 1e-5, lat=np.random.rand(96, 1) * 1e-5, time=simStart) else: # ==== forward simulation ==== # if repeatdtFlag: pset = ParticleSet(fieldset=fieldset, pclass=JITParticle, lon=np.random.rand(96, 1) * 1e-5, lat=np.random.rand(96, 1) * 1e-5,
def test_mitgridindexing_3D(mode, gridindexingtype, withtime): xdim = zdim = 201 ydim = 2 a = c = 20000 # domain size b = 2 lon = np.linspace(-a / 2, a / 2, xdim, dtype=np.float32) lat = np.linspace(-b / 2, b / 2, ydim, dtype=np.float32) depth = np.linspace(-c / 2, c / 2, zdim, dtype=np.float32) dx, dz = lon[1] - lon[0], depth[1] - depth[0] omega = 2 * np.pi / delta(days=1).total_seconds() if withtime: time = np.linspace(0, 24 * 60 * 60, 10) dimensions = {"lon": lon, "lat": lat, "depth": depth, "time": time} dsize = (time.size, depth.size, lat.size, lon.size) else: dimensions = {"lon": lon, "lat": lat, "depth": depth} dsize = (depth.size, lat.size, lon.size) index_signs = {'nemo': -1, 'mitgcm': 1} isign = index_signs[gridindexingtype] def calc_r_phi(ln, dp): return np.sqrt(ln**2 + dp**2), np.arctan2(ln, dp) def calculate_UVWR(lat, lon, depth, dx, dz, omega): U = np.zeros(dsize, dtype=np.float32) V = np.zeros(dsize, dtype=np.float32) W = np.zeros(dsize, dtype=np.float32) R = np.zeros(dsize, dtype=np.float32) for i in range(lon.size): for j in range(lat.size): for k in range(depth.size): r, phi = calc_r_phi(lon[i], depth[k]) if withtime: R[:, k, j, i] = r else: R[k, j, i] = r r, phi = calc_r_phi(lon[i] + isign * dx / 2, depth[k]) if withtime: W[:, k, j, i] = -omega * r * np.sin(phi) else: W[k, j, i] = -omega * r * np.sin(phi) r, phi = calc_r_phi(lon[i], depth[k] + dz / 2) if withtime: U[:, k, j, i] = omega * r * np.cos(phi) else: U[k, j, i] = omega * r * np.cos(phi) return U, V, W, R U, V, W, R = calculate_UVWR(lat, lon, depth, dx, dz, omega) data = {"U": U, "V": V, "W": W, "R": R} fieldset = FieldSet.from_data(data, dimensions, mesh="flat", gridindexingtype=gridindexingtype) fieldset.U.interp_method = "cgrid_velocity" fieldset.V.interp_method = "cgrid_velocity" fieldset.W.interp_method = "cgrid_velocity" def UpdateR(particle, fieldset, time): particle.radius = fieldset.R[time, particle.depth, particle.lat, particle.lon] class MyParticle(ptype[mode]): radius = Variable('radius', dtype=np.float32, initial=0.) radius_start = Variable('radius_start', dtype=np.float32, initial=fieldset.R) pset = ParticleSet(fieldset, pclass=MyParticle, depth=4e3, lon=0, lat=0, time=0) pset.execute(pset.Kernel(UpdateR) + AdvectionRK4_3D, runtime=delta(hours=14), dt=delta(minutes=5)) assert np.allclose(pset.radius, pset.radius_start, atol=10)
# initial=attrgetter('lon')) # prev_lat = Variable('prev_lat', dtype=np.float32, to_write=False, # initial=attrgetter('lat')) # #Now I also want the particle to be deleted if it is on land (so it won't move) # count=Variable('count',initial=0,to_write=False) # init_lon = Variable('init_lon', dtype=np.float32, to_write=False, # initial=attrgetter('lon')) # init_lat = Variable('init_lat', dtype=np.float32, to_write=False, # initial=attrgetter('lat')) #The starting point of the similation and the endtime print 'Creating the pset' starttime = datetime(2002, 1, 1, 0, 0) endtime = datetime(2014, 12, 31, 21, 0) pset = ParticleSet(fieldset=fieldset, pclass=SampleParticle, lon=lons, lat=lats, time=starttime) #%% All the different functions/kernels we want to have def DeleteParticle(particle, fieldset, time, dt): particle.delete() print 'we deleted it at ' + str(particle.lon) + ' and ' + str(particle.lat) def AgeSample(particle, fiedset, time, dt): current_time = particle.time timedifference = current_time - particle.prev_time particle.Age += timedifference particle.prev_time = current_time
(a * X - b * Y)) * (X * (X - 1) * Y * (Y - 1))**2 u4 = u4 * U0 v4 = 2 * (Y * (Y - 1))**2 * (X * (X - 1)**2 + X**2 * (X - 1)) * np.sin( np.pi * (a * X - b * Y)) + a * np.pi * np.cos(np.pi * (a * X - b * Y)) * (X * (X - 1) * Y * (Y - 1))**2 v4 = v4 * U0 particle.lon += (u1 + 2 * u2 + 2 * u3 + u4) / 6. * particle.dt particle.lat += (v1 + 2 * v2 + 2 * v3 + v4) / 6. * particle.dt psetRK4_0 = ParticleSet(fieldset, pclass=ScipyParticle, lon=partX, lat=partY) output = psetRK4_0.ParticleFile( name=mainpath + 'tests/bilinear_interp_correction/2D_case_cgrid_RK4_exact.nc', outputdt=1) psetRK4_0.execute(Advection2Dcorr_exact, dt=0.1, runtime=1000, output_file=output) output.close() #RK4 velocities + not corrected psetRK4_1 = ParticleSet(fieldset, pclass=ScipyParticle, lon=partX, lat=partY) output = psetRK4_1.ParticleFile( name=mainpath + 'tests/bilinear_interp_correction/2D_case_cgrid_RK4_VNC.nc',