def test18(self): instance = QGmodel() dx = instance.parameters.dx dy = instance.parameters.dy dx1, dy1 = instance.grid.cellsize() self.assertEqual(dx, dx1) self.assertEqual(dy, dy1) instance.stop()
def test10(self): instance = QGmodel() org = instance.boundaries(1) self.assertEquals(org.psi.number, 0.) self.assertEquals(org.dpsi_dt.number, 0.) shape = org.shape copy = org.copy() instance.stop()
def reference(tend=1. | units.hour, dt=3600. | units.s): q = QGmodel(redirection="none") q.parameters.dt = dt q.evolve_model(tend) psi = q.grid[:, :, 0].psi.number return psi
def test22(self): instance = QGmodel(redirection="none") wind_field = instance.wind_field.copy() self.assertEquals(instance.get_name_of_current_state(), 'RUN') self.assertEqual(wind_field.shape, (401, 401)) pi = 3.14159265358979 #expected=cos(2.*pi*((j-1.)/(Ny-1.)-0.5))+2.*sin(pi*((j-1.)/(Ny-1.)-0.5)) Ly = instance.parameters.Ly tau = instance.parameters.tau expected = tau * (numpy.cos(2 * pi * (wind_field.y / Ly - 0.5)) + 2. * numpy.sin(pi * (wind_field.y / Ly - 0.5))) self.assertAlmostEquals(wind_field.tau_x, expected, 7)
def test19(self): instance = QGmodel() cellsize = instance.grid.cellsize() self.assertEquals(instance.get_name_of_current_state(), 'EDIT') instance.parameters.boundary_west = "interface" self.assertEquals(instance.get_name_of_current_state(), 'CHANGE_PARAMETERS_EDIT') instance.recommit_parameters() self.assertEquals(instance.get_name_of_current_state(), 'EDIT') instance.stop()
def test2(self): instance = QGmodel(redirection="none") instance.grid[0, 0, 0].psi self.assertEquals(instance.get_name_of_current_state(), 'EDIT') instance.initialize_grid() self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.stop()
def test1(self): instance = QGmodel() instance.evolve_model(1 | units.hour) self.assertEquals(instance.get_name_of_current_state(), 'EVOLVED') psi = instance.grid.psi self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.stop()
def test6(self): instance = QGmodel() Nx, Ny, Nm = instance.grid.shape self.assertEqual(401, Nx) minx, maxx, miny, maxy, minz, maxz = instance.get_index_range_inclusive( ) self.assertEqual(minx, 1) self.assertEqual(miny, 1) self.assertEqual(minz, 1) self.assertEqual(maxx, Nx) self.assertEqual(maxy, Ny) self.assertEqual(maxz, Nm) instance.stop()
def test17(self): instance = QGmodel() self.assertEqual(instance.parameters.position_x, 0. | units.m) self.assertEqual(instance.parameters.position_y, 0. | units.m) instance.parameters.position_y = 123456. | units.m instance.parameters.position_x = 543216. | units.m self.assertEqual(instance.parameters.position_y, 123456. | units.m) self.assertEqual(instance.parameters.position_x, 543216. | units.m) x = instance.grid[0, 0, 0].x y = instance.grid[0, 0, 0].y self.assertEqual(y, 123456. | units.m) self.assertEqual(x, 543216. | units.m) i, j = instance.get_index_of_position(x, y) self.assertEqual(i, 1) self.assertEqual(j, 1) instance.stop()
def test23(self): instance = QGmodel(redirection="none") Lx = instance.parameters.Lx / 2 instance.parameters.Lx = Lx instance2 = QGmodel(redirection="none") print(instance.parameters.copy()) instance2.parameters.reset_from_memento(instance.parameters) self.assertEqual(instance2.parameters.Lx, Lx) instance.stop() instance2.stop()
def test24(self): instance = QGmodel() instance.parameters.timestep_method = "euler" instance.parameters.raw_parameter = 0.123 instance.commit_parameters() b1 = instance.get_timestep_method() self.assertEqual(b1, "euler") b1 = instance.get_raw_alpha() self.assertEqual(b1, 0.123) instance.stop()
def test14(self): instance = QGmodel() instance.parameters.boundary_south = "interface" instance.commit_parameters() b1, b2, b3, b4 = instance.get_boundary_conditions() self.assertEqual(b3, "interface") instance.stop()
def test5(self): instance = QGmodel(redirection="none") instance.before_get_parameter() dt = instance.parameters.dt instance.evolve_model(dt) dpsi_dt1 = instance.grid.dpsi_dt psi1 = instance.grid.psi instance.evolve_model(2 * dt) psi2 = instance.grid.psi dpsi_dt2 = instance.grid.dpsi_dt dpsi_estimate = ((psi2 - psi1) / dt).mean() dpsi = 0.5 * (dpsi_dt1 + dpsi_dt2).mean() self.assertTrue(abs(dpsi - dpsi_estimate) / dpsi < 0.1) instance.stop()
def viebahn2014(N=50, reynolds_number=1, dm=0.04): beta0 = 1.8616e-11 | (units.m * units.s)**-1 L = 1.e6 | units.m H = 4000. | units.m rho = 1000. | units.kg / units.m**3 dx = L / N A_H = beta0 * (dm * L)**3 tau = reynolds_number * A_H * rho * beta0 * H U = tau / beta0 / rho / H / L delta_m = (A_H / beta0)**(1. / 3) / L delta_i = (U / beta0)**0.5 / L timescale = 1. / (beta0 * L) print("Viebahn 2014 setup") print("N=%i, Reynolds_number=%f" % (N, reynolds_number)) print("dm (derived):", (A_H / beta0)**(1. / 3) / L) print("tau:", tau.value_in(units.Pa)) print("A:", A_H) print("timescale:", timescale.in_(units.s)) print("delta_m:", delta_m) print("delta_i:", delta_i) qg = QGmodel(redirection="none") qg.parameters.Lx = L qg.parameters.Ly = L qg.parameters.dx = dx qg.parameters.dy = dx qg.parameters.dt = 900 | units.s qg.parameters.A_H = A_H qg.parameters.interface_wind = True qg.parameters.rho = rho qg.parameters.beta0 = beta0 qg.parameters.ocean_depth = H qg.parameters.tau = tau def wind_function(x, y): return single_gyre_wind_model(x, y, L, tau) qg.set_wind(wind_function) return qg
def test15(self): instance = QGmodel(redirection="none") dx = instance.parameters.dx Lx = instance.parameters.Lx instance.grid[1:-1, 1:-1, 0].psi = 1. | units.m**2 / units.s psi, dpsi = instance.get_psi_state_at_point(dx / 2, Lx / 2., Lx / 2.) self.assertEqual(psi, 1. | units.m**2 / units.s) psi, dpsi = instance.get_psi_state_at_point(dx, Lx / 2., Lx / 2.) self.assertEqual(psi, 1. | units.m**2 / units.s) psi, dpsi = instance.get_psi_state_at_point(2 * dx, Lx / 2., Lx / 2.) self.assertEqual(psi, 1. | units.m**2 / units.s) psi, dpsi = instance.get_psi_state_at_point(dx, dx / 2., Lx / 2.) self.assertEqual(psi, 0.5 | units.m**2 / units.s) psi, dpsi = instance.get_psi_state_at_point(dx, dx / 2., dx / 2.) self.assertEqual(psi, 0.25 | units.m**2 / units.s) instance.stop()
def test3(self): instance = QGmodel(redirection="none") dt = 0.123 | units.hour instance.parameters.dt = dt instance.evolve_model(2 * dt) self.assertEqual(instance.model_time, 2 * dt) instance.stop()
def test8(self): instance = QGmodel(redirection="none") Lx2 = instance.parameters.Lx / 2 instance.parameters.Ly = Lx2 Nx, Ny, Nm = instance.grid.shape s = instance.boundaries(1).shape self.assertEqual(s, (2, Ny + 2, 1)) s = instance.boundaries(2).shape self.assertEqual(s, (2, Ny + 2, 1)) s = instance.boundaries(3).shape self.assertEqual(s, (Nx + 2, 2, 1)) s = instance.boundaries(4).shape self.assertEqual(s, (Nx + 2, 2, 1)) instance.stop()
def test12(self): instance = QGmodel() instance.parameters.Ly = instance.parameters.Ly / 16 instance.parameters.Lx = instance.parameters.Lx / 16 Nx, Ny, Nm = instance.grid.shape ix = numpy.arange(Nx) + 1 iy = numpy.arange(Ny) + 1 onesx = numpy.ones(Nx) onesy = numpy.ones(Ny) i1 = numpy.outer(ix, onesx).flatten() j1 = numpy.outer(onesy, iy).flatten() x = instance.grid.x.flatten() y = instance.grid.y.flatten() i, j = instance.get_index_of_position(x, y) self.assertEquals(i - i1, 0.) self.assertEquals(j - j1, 0.) instance.stop()
def test16(self): instance = QGmodel() dx = instance.parameters.dx instance.evolve_model(1 | units.hour) x = instance.grid[:, 10, 0].x y = instance.grid[:, 10, 0].y dpsi1 = instance.grid[:, 10, 0].dpsi_dt psi2, dpsi2 = instance.get_psi_state_at_point(0. * x + dx, x, y) self.assertAlmostEquals(dpsi1, dpsi2) instance.stop()
def test7(self): instance = QGmodel() Nx, Ny, Nm = instance.grid.shape self.assertEqual(401, Nx) minx, maxx, miny, maxy, minz, maxz = instance.get_boundary_index_range_inclusive( 1) self.assertEqual(minx, 0) self.assertEqual(miny, 0) self.assertEqual(minz, 1) self.assertEqual(maxx, 1) self.assertEqual(maxy, Ny + 1) self.assertEqual(maxz, Nm) minx, maxx, miny, maxy, minz, maxz = instance.get_boundary_index_range_inclusive( 2) self.assertEqual(minx, Nx) self.assertEqual(miny, 0) self.assertEqual(minz, 1) self.assertEqual(maxx, Nx + 1) self.assertEqual(maxy, Ny + 1) self.assertEqual(maxz, Nm) minx, maxx, miny, maxy, minz, maxz = instance.get_boundary_index_range_inclusive( 3) self.assertEqual(minx, 0) self.assertEqual(miny, 0) self.assertEqual(minz, 1) self.assertEqual(maxx, Nx + 1) self.assertEqual(maxy, 1) self.assertEqual(maxz, Nm) minx, maxx, miny, maxy, minz, maxz = instance.get_boundary_index_range_inclusive( 4) self.assertEqual(minx, 0) self.assertEqual(miny, Ny) self.assertEqual(minz, 1) self.assertEqual(maxx, Nx + 1) self.assertEqual(maxy, Ny + 1) self.assertEqual(maxz, Nm) instance.stop()
def test11(self): instance = QGmodel(redirection="none") instance.parameters.Ly = instance.parameters.Ly / 8 instance.parameters.Lx = instance.parameters.Lx / 8 for i in [1, 2, 3, 4]: org = instance.boundaries(i) shape = org.shape copy = org.copy() channel = copy.new_channel_to(org) copy.psi = numpy.random.random(shape) | units.m**2 / units.s copy.dpsi_dt = numpy.random.random(shape) | units.m**2 / units.s**2 channel.copy_attributes(["psi", "dpsi_dt"]) self.assertEquals(copy.psi, instance.boundaries(i).psi) self.assertEquals(copy.dpsi_dt, instance.boundaries(i).dpsi_dt) instance.stop()
def interface_bc(tend=1. | units.hour, dt=3600. | units.s, correct=True): q = QGmodel(redirection="none") q.parameters.dt = dt q.parameters.xbound1 = "interface" while q.model_time < tend: q.evolve_model(q.model_time + dt) if correct: xb1 = q.boundaries(1).copy() xb1.psi[0, 1:-1, 0] = -q.grid.psi[1, :, 0] channel = xb1.new_channel_to(q.boundaries(1)) channel.copy() psi = q.grid[:, :, 0].psi.number return psi
def test4(self): instance = QGmodel(redirection="none") instance.grid[0, 0, 0].dpsi_dt self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.stop()
def test9(self): instance = QGmodel() org = instance.grid copy = org.copy() instance.stop()
def test21(self): instance = QGmodel() self.assertFalse(instance.parameters.interface_wind) instance.parameters.interface_wind = True self.assertTrue(instance.parameters.interface_wind) instance.stop()
def test20(self): instance = QGmodel() cellsize = instance.grid.cellsize() self.assertEquals(instance.get_name_of_current_state(), 'EDIT') a = instance.parameters.robert_asselin_alpha instance.parameters.robert_asselin_alpha = 12345. self.assertEquals(instance.get_name_of_current_state(), 'CHANGE_PARAMETERS_EDIT') self.assertEqual(instance.parameters.robert_asselin_alpha, 12345.) instance.cleanup_code() self.assertEquals(instance.get_name_of_current_state(), 'END') instance.initialize_code() self.assertEqual(instance.parameters.robert_asselin_alpha, a) self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED') instance.stop()
def semi_domain_test(tend=1. | units.hour, dt=3600. | units.s): q1 = QGmodel(redirection="none") q1.parameters.dt = dt / 2 Lx = q1.parameters.Lx.value_in(1000 * units.km) q2 = QGmodel(redirection="none") q2.parameters.dt = dt / 2 q2.parameters.Lx /= 2 q2.parameters.boundary_east = "interface" Nx, Ny, Nm = q1.grid.shape pyplot.ion() f = pyplot.figure(figsize=(12, 5)) pyplot.show() i = 0 while q1.model_time < tend: i = i + 1 tnow = q1.model_time q1.evolve_model(tnow + dt / 2) psi = q1.grid[(Nx - 1) / 2:(Nx - 1) / 2 + 2, :, 0].psi dpsi_dt = q1.grid[(Nx - 1) / 2:(Nx - 1) / 2 + 2, :, 0].dpsi_dt west = q2.boundaries("west").copy() west[:, 1:-1, 0].psi = psi west[:, 1:-1, 0].dpsi_dt = dpsi_dt channel = west.new_channel_to(q2.boundaries("west")) channel.copy() q1.evolve_model(tnow + dt) q2.evolve_model(tnow + dt) # print(q1.grid[(Nx+1)/2,1,0].dpsi_dt) # print(q2.boundaries("west")[0:2,0,0].x) if i % 5 == 0: psi1_complete = q1.grid.psi.number[:, :, 0] psi1 = q1.grid[:(Nx + 1) / 2, :, 0].psi.number psi2 = q2.grid[:, :, 0].psi.number d = abs(psi2 - psi1) print(d.max(), d.mean(), abs(psi1).max()) f.clf() f1 = pyplot.subplot(131) f1.imshow(psi1_complete.transpose() / psi1.max(), vmin=0, vmax=1, extent=[0, Lx, 0, Lx], origin="lower") f1.set_xlabel("x (x1000 km)") f2 = pyplot.subplot(132) f2.imshow(psi2.transpose() / psi1.max(), vmin=0, vmax=1, extent=[0, Lx / 2, 0, Lx], origin="lower") f2.set_xlabel("x (x1000 km)") f3 = pyplot.subplot(133) f3.imshow(d.transpose() / psi1.max(), vmin=0, vmax=0.001, extent=[0, Lx / 2, 0, Lx], origin="lower") f3.set_xlabel("x (x1000 km)") pyplot.draw() pyplot.savefig("snapshots/half_domain_test-%6.6i.png" % i) raw_input()
def test0(self): instance = QGmodel() self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED') instance.initialize_code() self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED') instance.commit_parameters() self.assertEquals(instance.get_name_of_current_state(), 'EDIT') instance.evolve_model(1 | units.hour) self.assertEquals(instance.get_name_of_current_state(), 'EVOLVED') psi = instance.grid.psi self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.stop()
def high_level(): qg = QGmodel(redirection="none") qg.initialize_code() Nx = 128 Ny = 128 qg.parameters.Lx = 1.e6 | units.m qg.parameters.Ly = 1.e6 | units.m qg.parameters.dx = qg.parameters.Lx / Nx qg.parameters.dy = qg.parameters.Ly / Ny qg.parameters.ocean_depth = 600 | units.m qg.parameters.tau = 0.15 | units.Pa qg.parameters.beta0 = 1.6e-11 | (units.m * units.s)**-1 qg.parameters.dt = 1800 / 10 | units.s rho0 = qg.parameters.rho beta0 = qg.parameters.beta0 H = qg.parameters.ocean_depth L = qg.parameters.Lx T = qg.parameters.tau U_dijkstra = 1.6e-2 | units.m / units.s U = (T / (beta0 * rho0 * H * L)) print "actual, target U:", U.in_(units.m / units.s), U_dijkstra Reynolds_number = 1. A_H = U * L / Reynolds_number qg.parameters.A_H = A_H timescale = 1 / (beta0 * L) print "timescale:", timescale.in_(units.s) print qg.parameters qg.commit_parameters() # qg.grid.psi=(qg.grid.x/L ) | units.m**2/units.s qg.initialize_grid() pyplot.ion() f = pyplot.figure() pyplot.show() for i in range(101): tend = i * 12. | units.hour qg.evolve_model(tend) psi = qg.grid.psi[:, :, 0] print qg.model_time.in_(units.day), psi.max(), psi.min() pyplot.imshow(psi.transpose() / psi.max(), vmin=0, vmax=1, origin="lower") pyplot.draw() raw_input()
def sample_run(Nx=128, Ny=128, Reynolds_number=50., wind_sigma=1., dtplot=24. | units.hour, nstep=10): qg = QGmodel(redirection="none") qg.initialize_code() L = 1.e6 | units.m qg.parameters.Lx = L qg.parameters.Ly = L qg.parameters.dx = qg.parameters.Lx / Nx qg.parameters.dy = qg.parameters.Ly / Ny qg.parameters.ocean_depth = 600 | units.m qg.parameters.tau = 0.15 | units.Pa qg.parameters.beta0 = 1.6e-11 | (units.m * units.s)**-1 qg.parameters.dt = 900 | units.s rho0 = qg.parameters.rho beta0 = qg.parameters.beta0 H = qg.parameters.ocean_depth L = qg.parameters.Lx T = qg.parameters.tau U_dijkstra = 1.6e-2 | units.m / units.s # mentioned in paper U = (T / (beta0 * rho0 * H * L)) # actual derived from parameters print("actual, target U:", U.in_(units.m / units.s), U_dijkstra) A_H = U * L / Reynolds_number qg.parameters.A_H = A_H qg.parameters.wind_sigma = wind_sigma timescale = 1 / (beta0 * L) print("timescale:", timescale.in_(units.s)) print() print(qg.parameters) raw_input() qg.commit_parameters() # qg.grid.psi=(qg.grid.x/L ) | units.m**2/units.s qg.initialize_grid() pyplot.ion() f = pyplot.figure() pyplot.show() for i in range(nstep + 1): tend = i * dtplot qg.evolve_model(tend) psi = qg.grid.psi[:, :, 0] print(qg.model_time.in_(units.day),(psi.max()/L).in_(units.km/units.hour), \ (psi.min()/L).in_(units.km/units.hour)) f.clf() extent = [0, L.value_in(units.km), 0, L.value_in(units.km)] pyplot.imshow(psi.transpose() / max(psi.max(), -psi.min()), vmin=-1, vmax=1, origin="lower", extent=extent) pyplot.xlabel("km") pyplot.title(str(qg.model_time.in_(units.day))) pyplot.draw() raw_input()