Skip to content

Commit

Permalink
"autoupdate" now in place (almost) everywhere. Tests needed. Support …
Browse files Browse the repository at this point in the history
…pickle?
  • Loading branch information
mrambausek committed Feb 3, 2022
1 parent 4b8f193 commit 3d52ecd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
9 changes: 0 additions & 9 deletions comp/fespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,6 @@ namespace ngcomp
lot of new non-zero entries in the matrix!\n" << endl;
// else *testout << "\n (" << order << ") flag dgjumps is not used!" << endl;

if (autoupdate)
{
ma->updateSignal.Connect(this, [this]()
{
this->Update();
this->FinalizeUpdate();
});
}

if(flags.NumListFlagDefined("directsolverdomains"))
{
directsolverclustered.SetSize(ama->GetNDomains());
Expand Down
2 changes: 0 additions & 2 deletions comp/gridfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ namespace ngcomp
visual = !flags.GetDefineFlag ("novisual");
multidim = int (flags.GetNumFlag ("multidim", 1));
autoupdate = flags.GetDefineFlag ("autoupdate");
if (autoupdate)
fes->updateSignal.Connect(this, [this]() { this->Update(); });

auto comp_space = dynamic_pointer_cast<CompoundFESpace>(fespace);
if(comp_space)
Expand Down
38 changes: 29 additions & 9 deletions comp/python_comp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ namespace ngcomp
}




class PyNumProc : public NumProc
{
public:
Expand Down Expand Up @@ -564,6 +562,7 @@ kwargs : kwargs
fes->FinalizeUpdate();
if (!spaces[0]->DoesAutoUpdate())
fes->SetDoSubspaceUpdate(true);
connect_auto_update(fes.get());
return shared_ptr<FESpace>{fes};
// py::cast(*instance).attr("flags") = bpflags;
}),
Expand All @@ -579,6 +578,7 @@ kwargs : kwargs
auto fes = CreateFESpace (type, ma, flags);
fes->Update();
fes->FinalizeUpdate();
connect_auto_update(fes.get());
return fes;
}),
py::arg("type"), py::arg("mesh"),
Expand Down Expand Up @@ -962,7 +962,7 @@ coupling_type : ngsolve.comp.COUPLING_TYPE

.def_property_readonly("couplingtype", [] (shared_ptr<FESpace> self)
{ return FlatArray<COUPLING_TYPE>(self->CouplingTypes()); })
.def_property_readonly("autoupdate", [] (shared_ptr<FESpace> self) {self->DoesAutoUpdate();})
.def_property_readonly("autoupdate", [] (shared_ptr<FESpace> self) {return self->DoesAutoUpdate();})
.def ("GetFE", [](shared_ptr<FESpace> self, ElementId ei) -> py::object
{
auto fe = shared_ptr<FiniteElement> (&self->GetFE(ei, global_alloc));
Expand Down Expand Up @@ -1117,7 +1117,7 @@ rho : ngsolve.fem.CoefficientFunction
flags.SetFlag ("dgjumps", space1->UsesDGCoupling() || space2->UsesDGCoupling());

if (space1->DoesAutoUpdate() != space2->DoesAutoUpdate())
throw Exception("Both spaces need same autoupdate setting.");
throw Exception("Both spaces need same autoupdate setting.");
flags.SetFlag ("autoupdate", space1->DoesAutoUpdate());

if(space1->LowOrderFESpacePtr() && space2->LowOrderFESpacePtr())
Expand All @@ -1137,7 +1137,8 @@ rho : ngsolve.fem.CoefficientFunction
productspace->Update();
productspace->FinalizeUpdate();
if (!space1->DoesAutoUpdate())
productspace->SetDoSubspaceUpdate(true);
productspace->SetDoSubspaceUpdate(true);
connect_auto_update(productspace.get());
return productspace;
})

Expand All @@ -1153,7 +1154,8 @@ rho : ngsolve.fem.CoefficientFunction
productspace->Update();
productspace->FinalizeUpdate();
if (!space->DoesAutoUpdate())
productspace->SetDoSubspaceUpdate(true);
productspace->SetDoSubspaceUpdate(true);
connect_auto_update(productspace.get());
return productspace;
})

Expand Down Expand Up @@ -1212,6 +1214,7 @@ rho : ngsolve.fem.CoefficientFunction
fes->FinalizeUpdate();
if (!autoupdate)
fes->SetDoSubspaceUpdate(true);
connect_auto_update(fes.get());
return fes;
}))
.def(py::pickle([] (py::object pyfes)
Expand Down Expand Up @@ -1311,7 +1314,8 @@ component : int
vecspace->Update();
vecspace->FinalizeUpdate();
if (!space->DoesAutoUpdate())
vecspace->SetDoSubspaceUpdate(true);
vecspace->SetDoSubspaceUpdate(true);
connect_auto_update(vecspace.get());
return vecspace;
}),py::arg("space"), py::arg("dim")=nullopt)
.def(py::pickle([] (py::object pyfes)
Expand Down Expand Up @@ -1347,7 +1351,8 @@ component : int
matspace->Update();
matspace->FinalizeUpdate();
if (!space->DoesAutoUpdate())
matspace->SetDoSubspaceUpdate(true);
matspace->SetDoSubspaceUpdate(true);
connect_auto_update(matspace.get());
return matspace;
}),py::arg("space"), py::arg("dim")=nullopt, py::arg("symmetric")=false, py::arg("deviatoric")=false)
;
Expand Down Expand Up @@ -1500,6 +1505,8 @@ used_idnrs : list of int = None
perfes = make_shared<PeriodicFESpace>(fes,flags,a_used_idnrs);
perfes->Update();
perfes->FinalizeUpdate();
// MR: is autoupdate contained in flags?
connect_auto_update(perfes.get());
return perfes;
}), py::arg("fespace"), py::arg("phase")=nullopt,
py::arg("use_idnrs")=py::list())
Expand Down Expand Up @@ -1578,10 +1585,13 @@ BND : boolean or None
disc_class
.def(py::init([disc_class] (shared_ptr<FESpace> & fes, py::kwargs kwargs)
{
auto flags = CreateFlagsFromKwArgs(kwargs, disc_class);
auto flags = CreateFlagsFromKwArgs(kwargs, disc_class);
// MR: Is this logic correct or should it be required that fes does autoupdate?
flags.SetFlag("autoupdate", flags.GetDefineFlagX("autoupdate").IsTrue() || fes->DoesAutoUpdate());
auto dcfes = make_shared<DiscontinuousFESpace>(fes, flags);
dcfes->Update();
dcfes->FinalizeUpdate();
connect_auto_update(dcfes.get());
return dcfes;
}), py::arg("fespace"))
/*
Expand Down Expand Up @@ -1640,9 +1650,12 @@ fespace : ngsolve.comp.FESpace
.def(py::init([disc_class] (shared_ptr<FESpace> & fes, py::kwargs kwargs)
{
auto flags = CreateFlagsFromKwArgs(kwargs, disc_class);
// MR: Is this logic correct or should it be required that fes does autoupdate?
flags.SetFlag("autoupdate", flags.GetDefineFlagX("autoupdate").IsTrue() || fes->DoesAutoUpdate());
auto hiddenfes = make_shared<HiddenFESpace>(fes, flags);
hiddenfes->Update();
hiddenfes->FinalizeUpdate();
connect_auto_update(hiddenfes.get());
return hiddenfes;
}), py::arg("fespace"))
;
Expand All @@ -1656,9 +1669,12 @@ fespace : ngsolve.comp.FESpace
.def(py::init([] (shared_ptr<FESpace> & fes)
{
Flags flags = fes->GetFlags();
// MR: Is this logic correct or should it be required that fes does autoupdate?
flags.SetFlag("autoupdate", flags.GetDefineFlagX("autoupdate").IsTrue() || fes->DoesAutoUpdate());
auto refes = make_shared<ReorderedFESpace>(fes, flags);
refes->Update();
refes->FinalizeUpdate();
connect_auto_update(refes.get());
return refes;
}), py::arg("fespace"))
/*
Expand Down Expand Up @@ -1732,6 +1748,8 @@ active_dofs : BitArray or None
dynamic_pointer_cast<CompressedFESpace>(ret)->SetActiveDofs(py::extract<shared_ptr<BitArray>>(active_dofs)());
ret->Update();
ret->FinalizeUpdate();
// MR: Does ret "inherit" the autoupdate from "fes"
connect_auto_update(ret.get());
return ret;
}), py::arg("fespace"), py::arg("active_dofs")=DummyArgument())
.def("SetActiveDofs", [](CompressedFESpace & self, shared_ptr<BitArray> active_dofs)
Expand Down Expand Up @@ -1865,6 +1883,7 @@ active_dofs : BitArray or None
flags.SetFlag("novisual");
auto gf = CreateGridFunction(fes, name, flags);
gf->Update();
connect_auto_update(gf.get());
return gf;
}), py::arg("space"), py::arg("name")="gfu",
"creates a gridfunction in finite element space")
Expand Down Expand Up @@ -1941,6 +1960,7 @@ active_dofs : BitArray or None
"the finite element space")
.def("Update", [](GF& self) { self.Update(); },
"update vector size to finite element space dimension after mesh refinement")
.def_property_readonly("autoupdate", [] (shared_ptr<GridFunction> self) {return self->DoesAutoUpdate();})

.def("Save", [](GF& self, string filename, bool parallel)
{
Expand Down
20 changes: 20 additions & 0 deletions comp/python_comp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,28 @@ namespace ngcomp
state[2].cast<Flags>());
fes->Update();
fes->FinalizeUpdate();
// MR: connect_auto_update?
return dynamic_pointer_cast<FESPACE>(fes);
};

void connect_auto_update(FESpace* fes) {
if (fes->weak_from_this().expired())
throw Exception("Given pointer is not managed by a shared ptr.");
if (fes->DoesAutoUpdate())
fes->GetMeshAccess()->updateSignal.Connect(fes, [fes]()
{
fes->Update();
fes->FinalizeUpdate();
});
}

void connect_auto_update(GridFunction* gf) {
if (gf->weak_from_this().expired())
throw Exception("Given pointer is not managed by a shared ptr.");
if (gf->DoesAutoUpdate())
gf->GetFESpace()->updateSignal.Connect(gf, [gf](){ gf->Update(); });
}

template <typename FES, typename BASE=FESpace>
auto ExportFESpace (py::module & m, string pyname, bool module_local = false)
{
Expand All @@ -69,6 +88,7 @@ namespace ngcomp
auto fes = make_shared<FES>(ma,flags);
fes->Update();
fes->FinalizeUpdate();
connect_auto_update(fes.get());
return fes;
}),py::arg("mesh"))

Expand Down

0 comments on commit 3d52ecd

Please sign in to comment.