Hello,
I wanted to know if it is possible to exchange the mesh in fenics without having to reinstantiate everything. Basically, the problem boils down to: Can I use one mesh, define a function space, functions and variational forms, (perhaps solve them), then exchange the underlying mesh, and solve the same equations again, but on the new mesh without having to define everything again?
What I want to do is not (necessarily) to change the geometry, but to change the number of nodes and cells.
I provide a MWE below, which shows that my naive approach of using a MeshEditor on an already defined mesh does not yield the desired results.
import numpy as np
from fenics import *
mesh = UnitSquareMesh(3, 3)
V = FunctionSpace(mesh, "CG", 1)
print(V.dim()) # 16
print(mesh.num_vertices()) # 16
print(hex(id(mesh))) # memory location
u = Function(V)
x = SpatialCoordinate(mesh)
u.vector()[:] = project(x[0], V).vector()[:]
editor = MeshEditor()
editor.open(mesh, "triangle", 2, 2)
editor.init_vertices(4)
editor.init_cells(2)
editor.add_vertex(0, np.array([0.0, 0.0]))
editor.add_vertex(1, np.array([1.0, 0.0]))
editor.add_vertex(2, np.array([0.0, 1.0]))
editor.add_vertex(3, np.array([1.0, 1.0]))
editor.add_cell(0, np.array([0, 1, 3], dtype=np.uintp))
editor.add_cell(1, np.array([0, 2, 3], dtype=np.uintp))
editor.close()
print(V.dim()) # still 16
print(mesh.num_vertices()) # 4
print(hex(id(mesh))) # same memory location as before
When you run the script, you can see that the memory location of the mesh stays the same, that the number of mesh vertices changes as desired, but the dimension of the corresponding CG1 function space V does not.
So is there any way to make fenics do what I want?
Thanks a lot for the help and best regards,
Sebastian