def test_compile_extension_module(): # This test should do basically the same as the docstring of the # compile_extension_module function in compilemodule.py. Remember # to update the docstring if the test is modified! code = """ #include <pybind11/pybind11.h> #include <petscvec.h> #include <dolfin/la/PETScVector.h> void PETSc_exp(Vec x) { assert(x); VecExp(x); } PYBIND11_MODULE(SIGNATURE, m) { m.def("PETSc_exp", &PETSc_exp); } """ ext_module = compile_cpp_code(code) local_range = MPI.local_range(MPI.comm_world, 10) x = cpp.la.create_vector(MPI.comm_world, local_range, [], 1) x_np = np.arange(float(local_range[1] - local_range[0])) with x.localForm() as lf: lf[:] = x_np ext_module.PETSc_exp(x) x_np = np.exp(x_np) x = x.getArray() assert (x == x_np).all()
def test_save_and_read_vector(tempdir): filename = os.path.join(tempdir, "vector.h5") # Write to file local_range = MPI.local_range(MPI.comm_world, 305) x = PETScVector(MPI.comm_world, local_range, [], 1) x[:] = 1.2 with HDF5File(MPI.comm_world, filename, "w") as vector_file: vector_file.write(x, "/my_vector") # Read from file with HDF5File(MPI.comm_world, filename, "r") as vector_file: y = vector_file.read_vector(MPI.comm_world, "/my_vector", False) assert y.size() == x.size() x.axpy(-1.0, y) assert x.norm(dolfin.cpp.la.Norm.l2) == 0.0
def test_save_and_read_vector(tempdir): filename = os.path.join(tempdir, "vector.h5") # Write to file local_range = MPI.local_range(MPI.comm_world, 305) x = PETSc.Vec() x.create(MPI.comm_world) x.setSizes((local_range[1] - local_range[0], None)) x.setFromOptions() x.set(1.2) with HDF5File(MPI.comm_world, filename, "w") as vector_file: vector_file.write(x, "/my_vector") # Read from file with HDF5File(MPI.comm_world, filename, "r") as vector_file: y = vector_file.read_vector(MPI.comm_world, "/my_vector", False) assert y.getSize() == x.getSize() x.axpy(-1.0, y) assert x.norm() == 0.0
def test_compile_extension_module(): # This test should do basically the same as the docstring of the # compile_extension_module function in compilemodule.py. Remember # to update the docstring if the test is modified! from numpy import arange, exp code = """ #include <pybind11/pybind11.h> #include <petscvec.h> #include <dolfin/la/PETScVector.h> void PETSc_exp(std::shared_ptr<dolfin::la::PETScVector> vec) { Vec x = vec->vec(); assert(x); VecExp(x); } PYBIND11_MODULE(SIGNATURE, m) { m.def("PETSc_exp", &PETSc_exp); } """ ext_module = compile_cpp_code(code) local_range = MPI.local_range(MPI.comm_world, 10) vec = PETScVector(MPI.comm_world, local_range, [], 1) np_vec = vec.get_local() np_vec[:] = arange(len(np_vec)) vec[:] = np_vec ext_module.PETSc_exp(vec) np_vec[:] = exp(np_vec) assert (np_vec == vec.get_local()).all()