def test_ForcingCovariance_mult_parallel(my_ensemble, fs, fc, cov): "test that the multiplication method of ForcingCovariance can be called independently in an ensemble" fc.assemble() if my_ensemble.ensemble_comm.rank == 0: x = Function(fs).vector() x.set_local(np.ones(x.local_size())) y = Function(fs).vector() fc.mult(x, y) ygathered = y.gather() assert_allclose(ygathered, np.dot(cov, np.ones(nx + 1))) elif my_ensemble.ensemble_comm.rank == 1: x = Function(fs).vector() x.set_local(0.5 * np.ones(x.local_size())) y = Function(fs).vector() fc.mult(x, y) ygathered = y.gather() assert_allclose(ygathered, np.dot(cov, 0.5 * np.ones(nx + 1)))
def test_ForcingCovariance_mult(fs, fc, cov): "test the multiplication method of ForcingCovariance" fc.assemble() x = Function(fs).vector() x.set_local(np.ones(x.local_size())) y = Function(fs).vector() fc.mult(x, y) ygathered = y.gather() assert_allclose(ygathered, np.dot(cov, np.ones(nx + 1)))
def test_solve_forcing_covariance(comm, fs, A, b, fc, A_numpy, cov): "test solve_forcing_covariance" rhs = Function(fs).vector() rhs.set_local(np.ones(fc.get_nx_local())) ls = LinearSolver(A) result = solve_forcing_covariance(fc, ls, rhs) result_actual = result.gather() result_expected = np.linalg.solve(A_numpy, np.ones(nx + 1)) result_expected = np.dot(cov, result_expected) result_expected = np.linalg.solve(A_numpy, result_expected) assert_allclose(result_expected, result_actual, atol=1.e-10)
def test_InterpolationMatrix_interp_mesh_to_data(fs, coords, meshcoords): "test method to interpolate from distributed mesh to data gathered at root" # simple 1D test nd = len(coords) im = InterpolationMatrix(fs, coords) im.assemble() input_ordered = np.array([3., 2., 7., 4., 0., 0., 2., 1., 1., 1., 5.]) f = Function(fs).vector() meshcoords_ordered = np.linspace(0., 1., 11) with f.dat.vec as vec: imin, imax = vec.getOwnershipRange() for i in range(imin, imax): vec.setValue( i, input_ordered[np.where(meshcoords_ordered == meshcoords[i])]) if COMM_WORLD.rank == 0: expected = np.array([1., 0., 5.5, 3.25]) else: expected = np.zeros(0) out = im.interp_mesh_to_data(f) assert_allclose(out, expected, atol=1.e-10) # failure due to bad input sizes mesh2 = UnitIntervalMesh(12) V2 = FunctionSpace(mesh2, "CG", 1) f2 = Function(V2).vector() f2.set_local(np.ones(f2.local_size())) with pytest.raises(AssertionError): im.interp_mesh_to_data(f2) im.destroy()
def test_solve_forcing_covariance_parallel(my_ensemble, comm, fs, A, b, fc, A_numpy, cov): "test that solve_forcing_covariance can be called independently from an ensemble process" if my_ensemble.ensemble_comm.rank == 0: rhs = Function(fs).vector() rhs.set_local(np.ones(fc.get_nx_local())) ls = LinearSolver(A) result = solve_forcing_covariance(fc, ls, rhs) result_actual = result.gather() result_expected = np.linalg.solve(A_numpy, np.ones(nx + 1)) result_expected = np.dot(cov, result_expected) result_expected = np.linalg.solve(A_numpy, result_expected) assert_allclose(result_expected, result_actual, atol=1.e-10) elif my_ensemble.ensemble_comm.rank == 1: rhs = Function(fs).vector() rhs.set_local(0.5 * np.ones(fc.get_nx_local())) ls = LinearSolver(A) result = solve_forcing_covariance(fc, ls, rhs) result_actual = result.gather() result_expected = np.linalg.solve(A_numpy, 0.5 * np.ones(nx + 1)) result_expected = np.dot(cov, result_expected) result_expected = np.linalg.solve(A_numpy, result_expected) assert_allclose(result_expected, result_actual, atol=1.e-10)