Permalink
Browse files

Merge branch 'bv_pickling' into 'master'

add pickling support for BaseVector

See merge request !220
  • Loading branch information...
mhochsteger committed Aug 17, 2017
2 parents 3f19e4f + a128c32 commit 0aa5e123fb99af9d144a73a762dc35365572ade3
Showing with 38 additions and 17 deletions.
  1. +1 −1 comp/python_comp.cpp
  2. +16 −3 linalg/python_linalg.cpp
  3. +1 −1 py_tutorials/CMakeLists.txt
  4. +12 −12 py_tutorials/pickling.py
  5. +8 −0 python/__init__.py
@@ -1386,7 +1386,7 @@ flags : dict
)raw_string"), py::dynamic_attr())
.def("__ngsid__", [] (shared_ptr<FESpace> self)
{ return reinterpret_cast<std::uintptr_t>(self.get()); } )
.def("__reduce__", [&] (py::object fes_obj)
.def("__reduce__", [] (py::object fes_obj)
{
auto setstate_args = py::make_tuple(fes_obj.attr("__dict__"));
py::tuple constructor_args;
@@ -15,16 +15,29 @@ void NGS_DLL_HEADER ExportNgla(py::module &m) {
;
m.def("CreateVVector",
[] (size_t s, bool is_complex, int es)
{ return shared_ptr<BaseVector> (CreateBaseVector(s,is_complex, es)); },
[] (size_t s, bool is_complex, int es) -> shared_ptr<BaseVector>
{ return CreateBaseVector(s,is_complex, es); },
"size"_a, "complex"_a=false, "entrysize"_a=1);
py::class_<BaseVector, shared_ptr<BaseVector>>(m, "BaseVector",
py::dynamic_attr() // add dynamic attributes
)
.def("__ngsid__", [] ( BaseVector & self)
{ return reinterpret_cast<std::uintptr_t>(&self); } )
.def("__reduce__", [] (py::object self_object)
{
auto constructor = py::module::import("ngsolve").attr("CreateBaseVector");
auto self = py::cast<shared_ptr<BaseVector>>(self_object);
auto entries = py::list();
if(self->IsComplex())
for(auto entry : self->FV<Complex>())
entries.append(py::cast(entry));
else
for(auto entry : self->FV<double>())
entries.append(py::cast(entry));
auto constructor_args = py::make_tuple(self->Size(),self->IsComplex(),self->EntrySize(),entries,self_object.attr("__dict__"));
return py::make_tuple(constructor, constructor_args);
})
.def("__str__", [](BaseVector &self) { return ToString<BaseVector>(self); } )
.def("__repr__", [](BaseVector &self) { return "basevector"; } )
.def_property_readonly("size", py::cpp_function( [] (BaseVector &self) { return self.Size(); } ) )
@@ -21,7 +21,7 @@ install(FILES
)
include(CTest)
foreach(py_test assemble.py bla.py poisson.py adaptive.py cmagnet.py mixed.py hybrid_mixed.py hybrid_dg.py taskmanager.py compound.py)
foreach(py_test assemble.py bla.py poisson.py adaptive.py cmagnet.py mixed.py hybrid_mixed.py hybrid_dg.py taskmanager.py compound.py pickling.py)
add_test(NAME ${py_test} COMMAND ${NETGEN_PYTHON_EXECUTABLE} ${py_test} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set_tests_properties ( ${py_test} PROPERTIES TIMEOUT ${NGS_TEST_TIMEOUT} )
endforeach()
@@ -4,23 +4,23 @@
mesh = Mesh (netgen.geom2d.unit_square.GenerateMesh(maxh=0.1))
v = FESpace ("h1ho", mesh, order=4, dirichlet=[1])
v2 = L2(mesh,order=2)
u = GridFunction (v)
data = [u.vec, u.vec, u.vec]
u2 = GridFunction(v)
vec = u.vec
data = [v,v2,u,u2,u.vec]
import pickle
# store same vector three times
pickler = pickle.Pickler(open ("1.dat", "wb"))
pickler.dump (data)
del pickler
# NgsPickler recognizes redundancies -> smaller files
pickler = NgsPickler(open ("2.dat", "wb"))
pickler.dump (data)
del pickler
unpickler = pickle.Unpickler(open("1.dat","rb"))
fes,fes2,w,w2,z = unpickler.load()
assert fes.mesh is fes2.mesh
assert w.space is w2.space
# Use NgsUnpickler to load data again
unpickler = NgsUnpickler(open ("2.dat", "rb"))
loaded_data = unpickler.load()
del unpickler
print(loaded_data)
assert len(z) == len(u.vec)
for i in range(len(u.vec)):
assert u.vec[i] == z[i]
@@ -72,6 +72,14 @@ def patched_new(class_t, *args,**kwargs):
comp.Periodic.__new__ = comp.CreatePeriodicFESpace
comp.Periodic.__init__ = __empty_init
# creator function for unpickling of BaseVector
def CreateBaseVector(size,iscomplex,entrysize, entries,_dict):
vec = la.CreateVVector(size,iscomplex,entrysize)
for i,val in enumerate(entries):
vec[i] = val
vec.__dict__ = _dict
return vec
def TmpRedraw(*args, **kwargs):
solve._Redraw(*args, **kwargs)
try:

0 comments on commit 0aa5e12

Please sign in to comment.