def test_fieldKh_SpatiallyVaryingBrownianMotion(mesh, mode, xdim=200, ydim=100): """Test SpatiallyVaryingDiffusion on a non-uniform diffusivity field with a linear gradient in one direction""" mesh_conversion = 1 / 1852. / 60 if mesh is 'spherical' else 1 fieldset = zeros_fieldset(mesh=mesh, xdim=xdim, ydim=ydim, mesh_conversion=mesh_conversion) Kh = np.zeros((ydim, xdim), dtype=np.float32) for x in range(xdim): Kh[:, x] = np.tanh(fieldset.U.lon[x] / fieldset.U.lon[-1] * 10.) * xdim / 2. + xdim / 2. + 100. grid = RectilinearZGrid(lon=fieldset.U.lon, lat=fieldset.U.lat, mesh=mesh) fieldset.add_field(Field('Kh_zonal', Kh, grid=grid)) fieldset.add_field(Field('Kh_meridional', Kh, grid=grid)) dKh_zonal_dx, _ = fieldset.Kh_zonal.gradient() _, dKh_meridional_dy = fieldset.Kh_meridional.gradient() fieldset.add_field(Field('dKh_zonal_dx', dKh_zonal_dx, grid=grid)) fieldset.add_field(Field('dKh_meridional_dy', dKh_meridional_dy, grid=grid)) if mesh is 'spherical': fieldset.dKh_zonal_dx.units = GeographicPolar() fieldset.dKh_meridional_dy.units = Geographic() npart = 100 runtime = delta(days=1) random.seed(1234) pset = ParticleSet(fieldset=fieldset, pclass=ptype[mode], lon=np.zeros(npart), lat=np.zeros(npart)) pset.execute(pset.Kernel(SpatiallyVaryingBrownianMotion2D), runtime=runtime, dt=delta(hours=1)) lats = np.array([p.lat for p in pset]) lons = np.array([p.lon for p in pset]) tol = 2000 * mesh_conversion # effectively 2000 m errors (because of low numbers of particles) assert np.allclose(np.mean(lons), 0, atol=tol) assert np.allclose(np.mean(lats), 0, atol=tol) assert (stats.skew(lons) > stats.skew(lats))
}, 'borV': { 'time': 'time', 'lat': 'lat', 'lon': 'lon' }, } #%% #Create the fieldset with the periodic halo and time extrapolation for the EKE print 'Creating the fieldset' fieldset = FieldSet.from_netcdf(filenames, variables, dimensions, allow_time_extrapolation=True) fieldset.add_periodic_halo(zonal=True) fieldset.uuss.units = GeographicPolar() fieldset.vuss.units = Geographic() #The starting coordinates of the Particles, for the North Pacific. They are generated #by the code NAgrid.py, graciously send to me by David. lons = np.load( '/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LonsTestgrid0_5.npy' ) lats = np.load( '/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LatsTestgrid0_5.npy' ) #lons, lats = np.meshgrid(lon,lat) lons[lons < 0] += 360 #And now we define what sort of particles we are actually dealing with class SampleParticle(JITParticle):
dimensions={ 'lon': 'lon_rho', 'lat': 'lat_rho' }, interp_method='linear', mesh='spherical', allow_time_extrapolation=True) fieldset.add_field(iso_psi_all) fieldset.add_field(source_id_psi) fieldset.add_field(sink_id_psi) fieldset.add_field(cdist) fieldset.add_field(cnormx) fieldset.add_field(cnormy) fieldset.cnormx_rho.units = GeographicPolar() fieldset.cnormy_rho.units = Geographic() # ADD THE PERIODIC BOUNDARY fieldset.add_constant('halo_west', -180.) fieldset.add_constant('halo_east', 180.) fieldset.add_periodic_halo(zonal=True) # ADD DIFFUSION if param['Kh']: fieldset.add_constant_field('Kh_zonal', param['Kh'], mesh='spherical') fieldset.add_constant_field('Kh_meridional', param['Kh'], mesh='spherical') # ADD MAXIMUM PARTICLE AGE (IF LIMITED AGE) fieldset.add_constant('max_age', param['max_age'])
'lat': 'latitude', 'lon': 'longitude', 'time': 'time' }, } fieldset = FieldSet.from_netcdf(filenames, variables, dimensions, allow_time_extrapolation=True) ############################################################################### # Adding the Stokes drift to the HYCOM currents # ############################################################################### if stokes == 0: fieldset.Ust.units = GeographicPolar() fieldset.Vst.units = Geographic() fieldset = FieldSet( U=fieldset.U + fieldset.Ust, V=fieldset.V + fieldset.Vst, ) ############################################################################### # Adding the border current, which applies for all scenarios except for 0 # ############################################################################### datasetBor = Dataset(dataInputdirec + 'boundary_velocities_HYCOM.nc') borU = datasetBor.variables['MaskUvel'][:] borU[borU != 0] = borU[borU != 0] / abs(borU[borU != 0]) borV = datasetBor.variables['MaskVvel'][:] borV[borV != 0] = borV[borV != 0] / abs(borV[borV != 0]) borMag = np.sqrt(np.square(borU) + np.square(borV))
'vwnd':'vwnd', 'borU':'MaskUvel', 'borV':'MaskVvel'} dimensions = {'U':{'time':'time','lat':'lat','lon':'lon'}, 'V':{'time':'time','lat':'lat','lon':'lon'}, 'uwnd':{'time':'time','lat':'latitude','lon':'longitude'}, 'vwnd':{'time':'time','lat':'latitude','lon':'longitude'}, 'borU':{'time':'time','lat':'lat','lon':'lon'}, 'borV': {'time':'time','lat':'lat','lon':'lon'}, } #%% #Create the fieldset with the periodic halo and time extrapolation for the EKE print 'Creating the fieldset' fieldset = FieldSet.from_netcdf(filenames, variables, dimensions,allow_time_extrapolation=True) fieldset.add_periodic_halo(zonal=True) fieldset.uwnd.units=GeographicPolar() fieldset.vwnd.units=Geographic() #The starting coordinates of the Particles, for the North Pacific. They are generated #by the code NAgrid.py, graciously send to me by David. lons=np.load('/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LonsTestgrid0_5.npy') lats=np.load('/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LatsTestgrid0_5.npy') #lons, lats = np.meshgrid(lon,lat) lons[lons<0]+=360 #And now we define what sort of particles we are actually dealing with class SampleParticle(JITParticle): # #Now the part to determine the age of the particle Age=Variable('Age',initial=0.,dtype=np.float32)#agr is gonna be in seconds prev_time=Variable('prev_time',initial=attrgetter('time'),to_write=False)