def test_variance1(start_loc, time_step): """ After a few timesteps the variance of the particle positions should be similar to the computed value: var = Dt """ num_le = 1000 start_time = datetime.datetime(2012, 11, 10, 0) sc = sample_sc_release(num_le, start_loc, start_time) D = 100000 num_steps = 10 rand = RandomMover(diffusion_coef=D) model_time = start_time for i in range(num_steps): model_time += datetime.timedelta(seconds=time_step) sc.release_elements(time_step, model_time) rand.prepare_for_model_step(sc, time_step, model_time) delta = rand.get_move(sc, time_step, model_time) # print "delta:", delta sc['positions'] += delta # print sc['positions'] # compute the variances: # convert to meters pos = FlatEarthProjection.lonlat_to_meters(sc['positions'], start_loc) var = np.var(pos, axis=0) # D converted to meters^s/s expected = 2.0 * (D * 1e-4) * num_steps * time_step assert np.allclose(var, (expected, expected, 0.), rtol=0.1)