Can i do this in FEnics??.
Also, i am very beginner. I want to know how i can learn FEncis codes for Finite element method and DG soon. I am using windows11. I have also installed FEnics with conda. Demo codes is running fine. But Matplotlib is not working for me specially for 3D plot. I have installed Matplotlib also. I have to calculate L2-error and its convergence also. I want to do simulation also. Kindly help for the above.
I need some demo codes in all three meshes.
Poisson equation ( linear and non-linear).
Heat equation with neumann boundary=0.
A couple equation with more then one unkowns ( like Cahn-Hilliard Equation).
I have to learn soon. I have very less time in my hands.
You can create mesh (a) using FEniCSx, but for more complicated meshes I would recommend using gmsh. You can generate an .msh file and then import it into FEniCSx.
Demo codes and more information on how to solve those equations and how to create the meshes can be found in the FEniCSx tutorial and in the python documentation. I’d suggest reading through those, these are excellent learning resources.
To me, it looks like you could use a uniform unit square (consisting of quadrilateral elements), and apply a deformation field to the nodes to get the second and third grid.
Thanks for your replies. Yes, my domain is D=[0,1]^2, after i want to descritize it by using Quadrilateral elements in 2D. But, i need structured uniform, structured non-uniform, and unstuctured meshes Codes as i have shared in the images.
I am very beginner in FEnics. So, Kindly please it will be very helpfull for me if you only provide the codes so that i can plot the three types of meshes. I am getting confused now with code that you shared. I need only codes to plot this three meshes and so that i can use them later in my code.
Please note that you have only provided images, leaving it to me to interpret what the spacing between each cell is.
Here I have extracted the relevant code from the post I previously referred to to illustrate how to do this:
"""
Move mesh based on geometrical function
Author: Jørgen S. Dokken and Henrik N.T. Finsberg
SPDX-License-Identifier: MIT
"""
from mpi4py import MPI
import dolfinx
import numpy as np
def vertex_to_dof_map_vectorized(V):
"""Create a map from the vertices of the mesh to the corresponding degree of freedom."""
mesh = V.mesh
num_vertices_per_cell = dolfinx.cpp.mesh.cell_num_entities(
mesh.topology.cell_type, 0
)
dof_layout2 = np.empty((num_vertices_per_cell,), dtype=np.int32)
for i in range(num_vertices_per_cell):
var = V.dofmap.dof_layout.entity_dofs(0, i)
assert len(var) == 1
dof_layout2[i] = var[0]
num_vertices = (
mesh.topology.index_map(0).size_local + mesh.topology.index_map(0).num_ghosts
)
c_to_v = mesh.topology.connectivity(mesh.topology.dim, 0)
assert (c_to_v.offsets[1:] - c_to_v.offsets[:-1] == c_to_v.offsets[1]).all(), (
"Single cell type supported"
)
vertex_to_dof_map = np.empty(num_vertices, dtype=np.int32)
vertex_to_dof_map[c_to_v.array] = V.dofmap.list[:, dof_layout2].reshape(-1)
return vertex_to_dof_map
def perturb_mesh(u: dolfinx.fem.Function):
"""Perturb the mesh based on the displacement function."""
V = u.function_space
mesh = V.mesh
tdim = mesh.topology.dim
vertex_to_dof_map = vertex_to_dof_map_vectorized(V)
mesh.topology.create_connectivity(0, tdim)
num_vertices = (
mesh.topology.index_map(0).size_local + mesh.topology.index_map(0).num_ghosts
)
vertex_to_node_map = dolfinx.mesh.entities_to_geometry(
mesh, 0, np.arange(num_vertices, dtype=np.int32)
).reshape(-1)
u_values = u.x.array.reshape(-1, mesh.geometry.dim)
gdim = mesh.geometry.dim
mesh.geometry.x[vertex_to_node_map, :gdim] += u_values[vertex_to_dof_map, :]
if __name__ == "__main__":
N = 25
# Create parent mesh and mock displacement
mesh = dolfinx.mesh.create_unit_square(
MPI.COMM_WORLD, N, N, cell_type=dolfinx.mesh.CellType.quadrilateral
)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
def expr(x):
move_1 = x[1] * (0.5 - x[1]) * (x[1] < 0.5)
move_2 = (x[1] - 1) * (x[1] - 0.5) * (x[1] > 0.5)
return (np.zeros(x.shape[1]), move_1 + move_2)
u = dolfinx.fem.Function(V)
u.interpolate(expr)
perturb_mesh(u)
# Create the dof -> vertex map and vertex->node map
with dolfinx.io.XDMFFile(mesh.comm, "moved_mesh.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
# Reset mesh
u.x.array[:] *= -1
perturb_mesh(u)
def expr2(x, eps=1e-8):
xi = np.floor(x[0] * N)
yi = np.floor(x[1] * N)
move_x = (
0.2
/ N
* np.cos(10 * np.pi * x[0])
* (x[1] > eps)
* (x[1] < 1 - eps)
* (-1) ** yi
)
move_y = (
0.2
/ N
* np.cos(10 * np.pi * x[1])
* (x[0] > eps)
* (x[0] < 1 - eps)
* (-1) ** xi
)
return (move_y, move_x)
u.interpolate(expr2)
perturb_mesh(u)
with dolfinx.io.XDMFFile(mesh.comm, "new_moved_mesh.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
you can of course adapt these movement fields to suite your needs.
For further questions, I would like you to make an effort and at least try to make a minimal example illustrating what you have tried, and what didn’t work.
Actually i have installed FEnics 2019 in conda which support only dolfin not dolfinx. Can please you tell that how can i install FEniCSx with dolfinx in windows via wsl??. I have run all most the demo codes that are FEnics tutorial in FEnics 2019 with dolfin but only HDG poisson codes created problem to run. What is the solution of it. Can i not work with FEnics 2019??. I am facing problem to install dolfinx with FEnicsx in windows wsl.
If you are using conda, you can install DOLFINx in a conda environment, as described in
You can adapt the code that I made above to work for legacy FEniCS. However, you cannot expect me to translate the code to a legacy code-base.
Secondly, quadrilateral elements have very limited support in legacy FEniCS, and I would strongly advice you to use DOLFINx.
You have not shown any of the issues you have gotten by providing a traceback, so it is really hard for anyone to give you any further guidance.
It seems like you are using conda to run dolfinx. Then you are using v0.9.0 of the code, and should therefore download the demo with the 0.9.0 compatible API.
Secondly, adding extra posts is not going to accelerate the answering process.