Hi,
Hello,
I want to use mesh refinement in FEniCS.
While searching on the internet I found two commands: adapt and refine.
To try to understand how these commands work I tried to program a refinement loop that refines the mesh around a given point.
I have two problems:
when using adapt (commented line in the code) I have an error while I added the lines from the post “Equivalent commmand of adapt(space) in fenics 2019”
ETraceback (most recent call last):
File “example_04.py”, line 52, in
mesh = adapt(mesh,markers)
TypeError: adapt(): incompatible function arguments. The following argument types are supported:
1. (mesh_function: dolfin.cpp.mesh.MeshFunctionSizet, adapted_mesh: dolfin.cpp.mesh.Mesh) → dolfin.cpp.mesh.MeshFunctionSizet
2. (bc: dolfin.cpp.fem.DirichletBC, adapted_mesh: dolfin.cpp.mesh.Mesh, S: dolfin.cpp.function.FunctionSpace) → dolfin.cpp.fem.DirichletBC
3. (space: dolfin.cpp.function.FunctionSpace, adapted_mesh: dolfin.cpp.mesh.Mesh) → dolfin.cpp.function.FunctionSpace
4. (form: dolfin.cpp.fem.Form, adapted_mesh: dolfin.cpp.mesh.Mesh, adapt_coefficients: bool = True) → dolfin.cpp.fem.Form
Invoked with: <dolfin.cpp.generation.UnitSquareMesh object at 0x7f808b9cd360>, <dolfin.cpp.mesh.MeshFunctionBool object at 0x7f808a745f70>
rror message:
If I use refine, I don’t understand why it is not the marked cell that is refined (In fact, it works for the first refinement, not for the following ones)
Here is the script I am using.
from fenics import *
from matplotlib import pyplot
cpp_code = “”"
#include<pybind11/pybind11.h>
#include<dolfin/adaptivity/adapt.h>
#include<dolfin/mesh/Mesh.h>
#include<dolfin/mesh/MeshFunction.h>
#include<dolfin/fem/DirichletBC.h>
#include<dolfin/function/FunctionSpace.h>
#include<dolfin/fem/Form.h>
namespace py = pybind11;
PYBIND11_MODULE(SIGNATURE, m) {
m.def("adapt", (std::shared_ptr<dolfin::MeshFunction<std::size_t>> (*)(const dolfin::MeshFunction<std::size_t>&,
std::shared_ptr<const dolfin::Mesh>)) &dolfin::adapt,
py::arg("mesh_function"), py::arg("adapted_mesh"));
m.def("adapt", (std::shared_ptr<dolfin::DirichletBC> (*)(const dolfin::DirichletBC&,
std::shared_ptr<const dolfin::Mesh>, const dolfin::FunctionSpace&)) &dolfin::adapt,
py::arg("bc"), py::arg("adapted_mesh"), py::arg("S"));
m.def("adapt", (std::shared_ptr<dolfin::FunctionSpace> (*)(const dolfin::FunctionSpace&,
std::shared_ptr<const dolfin::Mesh>)) &dolfin::adapt,
py::arg("space"), py::arg("adapted_mesh"));
m.def("adapt", (std::shared_ptr<dolfin::Form> (*)(const dolfin::Form&,
std::shared_ptr<const dolfin::Mesh>, bool)) &dolfin::adapt,
py::arg("form"), py::arg("adapted_mesh"), py::arg("adapt_coefficients")=true);
}
"""
adapt = compile_cpp_code(cpp_code).adapt
n_elem = 1
n_ref = 5
mesh = UnitSquareMesh(n_elem,n_elem,“crossed”)
Praf = Point(0.20981,0.10987652,0.0)
for i_ref in range(n_ref):
print(“-------------------------------------”)
print(“Refinement “+str(i_ref))
print(”-------------------------------------”)
print(" “)
markers = MeshFunction(“bool”, mesh, False)
for cell in cells(mesh):
markers[cell] = cell.contains(Praf)
if markers[cell]:
print(cell.get_vertex_coordinates())
print(”")
plot(mesh)
pyplot.show()
mesh = refine(mesh,markers)
mesh = adapt(mesh,markers)
plot(mesh)
pyplot.show()
Thank you very much.
Xavier