Hello, dokken. When I tried to create the mesh, I met the following problem.
(1) If I don’t use mpirun, the solution is correct. There is no error.
(2) If I use mpirun, the solution is wrong. In the script, I stack two intervals
xmesh = np.hstack((np.linspace(0, 0.5, 16), np.linspace(0.52, L, 16)))
If I print the grid points, I find two 0.5 and 0.52
[...0.4 0.43333333 0.46666667 0.5 0.5 0.52
0.52 0.61866667 0.71733333 0.816 0.91466667 1.0133333 ...]
and the solution is not the same at the two 0.5 and 0.52 points. Also when I use mpirun, I get a error at the end of the script.
munmap_chunk(): invalid pointer
(3) If I use MPI.COMM_SELF in the scipt, I get some errors like
mpirun -np 2 python3 test2.py
HDF5-DIAG: Error detected in HDF5 (1.14.3) MPI-process 1:
#000: H5F.c line 660 in H5Fcreate(): unable to synchronously create file
major: File accessibility
minor: Unable to create file
#001: H5F.c line 614 in H5F__create_api_common(): unable to create file
major: File accessibility
minor: Unable to open file
Here is my complete script
# -*- coding:utf-8 -*-
import ufl
import dolfinx
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver
from petsc4py import PETSc
from mpi4py import MPI
import numpy as np
t_ini = 0
t_fin = 2.0
n_step = 20
dt = (t_fin - t_ini) / n_step
nx = 32
L = 2.0
rank = MPI.COMM_WORLD.Get_rank()
xmesh = np.hstack((np.linspace(0, 0.5, 16), np.linspace(0.52, L, 16)))
xmesh = np.reshape(xmesh, (len(xmesh), 1))
connectivity = np.array([[i, i+1] for i in range(len(xmesh)-1)], dtype=np.int64)
c_el = ufl.Mesh(ufl.VectorElement("Lagrange", ufl.interval, 1))
domain = dolfinx.mesh.create_mesh(MPI.COMM_WORLD, connectivity, xmesh, c_el)
#domain = dolfinx.mesh.create_interval(MPI.COMM_WORLD, nx, points=(0.0, L))
V = dolfinx.fem.FunctionSpace(domain, ("Lagrange", 1))
fdim = domain.topology.dim - 1
boundary_facets = dolfinx.mesh.locate_entities_boundary(domain, fdim, lambda x: np.isclose(L, x[0]))
bc = dolfinx.fem.dirichletbc(PETSc.ScalarType(1.0), dolfinx.fem.locate_dofs_topological(V, fdim, boundary_facets), V)
u_n = dolfinx.fem.Function(V)
u_n.interpolate(lambda x: np.ones(x[0].shape))
u = dolfinx.fem.Function(V)
v = ufl.TestFunction(V)
a = u*v*ufl.dx + dt*ufl.dot(ufl.grad(u), ufl.grad(v))*ufl.dx + dt*ufl.sin(u)*v*ufl.ds
L = u_n*v*ufl.dx
F = a - L
problem = NonlinearProblem(F, u, bcs=[bc])
solver = NewtonSolver(MPI.COMM_WORLD, problem)
solver.convergence_criterion = "incremental"
solver.rtol = 1e-6
ksp = solver.krylov_solver
opts = PETSc.Options()
option_prefix = ksp.getOptionsPrefix()
opts[f"{option_prefix}ksp_type"] = "cg"
opts[f"{option_prefix}pc_type"] = "gamg"
opts[f"{option_prefix}pc_factor_mat_solver_type"] = "mumps"
ksp.setFromOptions()
xdmf = dolfinx.io.XDMFFile(domain.comm, "diffusion.xdmf", "w")
xdmf.write_mesh(domain)
t = t_ini
while t < t_fin:
n, converged = solver.solve(u)
if (rank == 0):
print(f"Number of interations: {n:d}")
u_n.x.array[:] = u.x.array
t += dt
xdmf.write_function(u, t)
xdmf.close()
And I use the following script to plot the solution:
import h5py
import numpy as np
import matplotlib.pyplot as plt
f = h5py.File("diffusion.h5")
x = np.array(f['Mesh']['mesh']['geometry'])
x = x[:, 0]
order = x.argsort()
x = x[order]
for t_key in f['Function']['f'].keys():
t = float(t_key)
u = np.array(f['Function']['f'][t_key][:, 0])
plt.plot(x, u[order], color="red")
plt.show()
f.close()
The solution become this if I run the script by mpirun -np 2 python3 script.py
