Hi again, Please find the code and the attached error:
(sorry don’t know how to quote it correctly, I use "Block quote )
Blockquote
from dolfinx.mesh import create_unit_square, create_box, CellType
from dolfinx import mesh, fem
from dolfinx.fem import locate_dofs_geometrical, Constant, form
import pyvista # visualizing the mesh using pyvista, an interface to the VTK toolkit.
print(pyvista.global_theme.jupyter_backend)
import dolfinx.plot as plot
from ufl import TrialFunction, TestFunction, dot, div, inner ,dx, SpatialCoordinate
import numpy as np
from petsc4py import PETSc
from slepc4py import SLEPc
import matplotlib.pyplot as plt
import math
from mpi4py import MPI
import sys, io, slepc4py, os.path
factor = math.pi*math.pi
slepc4py.init(sys.argv)
opts = PETSc.Options()
import dolfinx
print(f"DOLFINx version: {dolfinx.version} based on GIT commit: {dolfinx.git_commit_hash} of GitHub - FEniCS/dolfinx: Next generation FEniCS problem solving environment")
Blockquote
#==================
import gmsh
import math
import sys
import meshio
#==================
def lshape_unstructQuad_gmsh2(element, num_elem):
print(’ The number of elements:‘, num_elem)
if (num_elem%2) != 0:
print(f"Insurt an even number of elements, the given number is {num_elem}")
exit()
num_of_points = num_elem + 1
gmsh.initialize()
lc = 1e-2
# boundaries: [-1,1]x[-1,1]
p1 = gmsh.model.geo.addPoint(-1 , -1 , 0, lc, 1)
p2 = gmsh.model.geo.addPoint(0 , -1 , 0, lc, 2)
p3 = gmsh.model.geo.addPoint(0 , 0 , 0, lc, 3)
p4 = gmsh.model.geo.addPoint(1 , 0 , 0, lc, 4)
p5 = gmsh.model.geo.addPoint(1 , 1 , 0, lc, 5)
p6 = gmsh.model.geo.addPoint(-1 , 1 , 0, lc, 6)
input(‘The boundaries of the domain are [-1,-1]x[1,1]:’)
l1 = gmsh.model.geo.addLine(p1, p2, 1)
l2 = gmsh.model.geo.addLine(p2, p3, 2)
l3 = gmsh.model.geo.addLine(p3, p4, 3)
l4 = gmsh.model.geo.addLine(p4, p5, 4)
l5 = gmsh.model.geo.addLine(p5, p6, 5)
l6 = gmsh.model.geo.addLine(p6, p1, 6)
# Adding points, lines, Create surface
gmsh.model.geo.addCurveLoop([l1, l2, l3, l4, l5, l6], 1)
gmsh.model.geo.addPlaneSurface([1], 1)
gmsh.model.addPhysicalGroup(0, [1, 2, 3, 4, 5, 6], 1)
gmsh.model.addPhysicalGroup(1, [1, 2, 3, 4, 5, 6], 2)
gmsh.model.addPhysicalGroup(2, [1], 1)
gmsh.model.geo.mesh.setTransfiniteCurve(l1, int(num_of_points/2)+1)
gmsh.model.geo.mesh.setTransfiniteCurve(l2, int(num_of_points/2)+1)
gmsh.model.geo.mesh.setTransfiniteCurve(l3, int(num_of_points/2)+1)
gmsh.model.geo.mesh.setTransfiniteCurve(l4, int(num_of_points/2)+1)
gmsh.model.geo.mesh.setTransfiniteCurve(l5, num_of_points)
gmsh.model.geo.mesh.setTransfiniteCurve(l6, num_of_points)
# # To create quadrangles instead of triangles, one can use the `setRecombine’
# # constraint:
gmsh.model.geo.mesh.setRecombine(2, 1)
gmsh.model.geo.synchronize()
# Finally we apply an elliptic smoother to the grid to have a more regular
# mesh:
# gmsh.option.setNumber(“Mesh.Smoothing”, 100)
gmsh.model.mesh.generate(2)
#===============testing
# if ‘close’ not in sys.argv:
# gmsh.fltk.run() # Write mesh data:
# gmsh.write(“mymeshmsh”)
# gmsh.finalize()
#=============end testing
#-----------fenicsx
print(“Creating the mesh in fenicsx”)
from dolfinx.io import gmshio
from mpi4py import MPI
gmsh_model_rank = 0
mesh_comm = MPI.COMM_WORLD
domain, cell_markers, facet_markers = gmshio.model_to_mesh(gmsh.model,
mesh_comm,
gmsh_model_rank,
gdim=2)
print(‘done with the mesh…’)
total_num_of_elems = num_elem*num_elem
print(‘The number of elments in the L-shape domain:’,total_num_of_elems)
# Dimension of the space
tdim = domain.topology.dim
# print(tdim)
topology, cell_types, geometry = plot.create_vtk_mesh(domain, tdim)
print(‘Constructing the mesh …’)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True)
plotter.view_xy()
# To view the mesh
plotter.show()
print(‘done…’)
return domain, tdim, total_num_of_elems, topology, cell_types, geometry
Blockquote
num_elemnts = 6
RT_order = 1 # order=1 is RT_0 lowest order RT for xfenics
element = ‘lshape_unstr_quad’
print(“number of elements %i”%num_elemnts)
‘’’ The mesh’‘’
domain, tdim, total_num_of_elems, ,,_ = lshape_unstructQuad_gmsh2(element, num_elemnts)
‘’‘The function spaces’‘’
print(‘Constructing the space …’)
space = fem.FunctionSpace(domain, (“RT”, RT_order))
print(‘done …\n’)
‘’‘Trial and test functions’‘’
print(‘The trial and test functions…’)
u = TrialFunction(space)
v = TestFunction(space)
print(‘done…\n’)
‘’‘The operators and the bilinear form’‘’
‘’‘Left hand side’‘’
print(‘Constructing the bilinear forms…’)
a = div(u)div(v)dx
stiff_bilinear_form = fem.form(a)
‘’‘Right hand side’‘’
b = inner(u,v)dx
mass_bilinear_form = fem.form(b)
print(‘done…\n’)
‘’‘----------------Setting the BC---------------------’‘’
print(‘Impossing the BC…’)
‘’’ Create facet to cell connectivity required to determine boundary facets’‘’
tdim = domain.topology.dim
fdim = tdim - 1
domain.topology.create_connectivity(fdim, tdim)
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = fem.locate_dofs_topological(space, fdim, boundary_facets)
ubc = fem.Function(space)
ubc.vector.set(0.0)
bc = fem.dirichletbc(ubc, boundary_dofs)
local_range = space.dofmap.index_map.local_range
dofs = np.arange(local_range)
print(‘INFO :The Dofs ‘, dofs)
‘’‘Assempleing the matrices’’’
print(‘Assembling the system…’)
A = fem.petsc.create_matrix(stiff_bilinear_form, )
A.setOption(PETSc.Mat.Option.SYMMETRIC, True)
A.setOption(PETSc.Mat.Option.SYMMETRY_ETERNAL, True)
A.setOption(PETSc.Mat.Option.IGNORE_ZERO_ENTRIES, True)
fem.petsc.assemble_matrix(A, stiff_bilinear_form,bcs=[bc])
A.assemble()
A.setOption(PETSc.Mat.Option.NEW_NONZERO_LOCATIONS, False)
‘’’ Mass matrix ‘’’
B = fem.petsc.create_matrix(mass_bilinear_form)
B.setOption(PETSc.Mat.Option.SYMMETRIC, True)
B.setOption(PETSc.Mat.Option.SYMMETRY_ETERNAL, True)
B.setOption(PETSc.Mat.Option.IGNORE_ZERO_ENTRIES, True)
fem.petsc.assemble_matrix(B, mass_bilinear_form,bcs=[bc])
B.assemble()
B.setOption(PETSc.Mat.Option.NEW_NONZERO_LOCATIONS, False)
print(‘done…\n’)
‘’‘Imposing the zero boundary conditions ‘’’
print(‘Imposing the zero Boundary conditions’)
B.zeroRowsLocal(bc.dof_indices()[0], 1.)
print(‘done…\n’)
print(‘Setting the solver…’)
shift = SLEPc.ST().create(MPI.COMM_WORLD)
shift.setType(‘sinvert’) # spectral transform
shift.setShift(1/factor) # spectral shift
eigensolver = SLEPc.EPS().create(MPI.COMM_WORLD)
numb_eigs = 1000
eigensolver.setDimensions(numb_eigs) # set number of eigenvalues to compute
eigensolver.setProblemType(eigensolver.ProblemType.GNHEP)
eigensolver.setWhichEigenpairs(eigensolver.Which.TARGET_MAGNITUDE) # For shift-and-invert
eigensolver.setST(shift)
eigensolver.setOperators(A,B)
eigensolver.setFromOptions() #any options specified at run time in the command line are
print(‘done…\n’)
print(‘Solving the problem…’)
eigensolver.solve()
print(‘done…\n’)
print("“)
print(” SLEPc Solution Results “)
print(”****“)
print()
num_of_converged_eig_val = eigensolver.getConverged()
vr, vi = A.createVecs()
real_eigs_sorted =
loop = 0
print( “Number of converged eigenpairs %d” % num_of_converged_eig_val )
print(‘’)
if num_of_converged_eig_val > 0:
for i in range (num_of_converged_eig_val):
l = eigensolver.getEigenpair(i ,vr, vi)
if element == ‘lshape_unstr_quad’:
if l.real > 1.4:
print(f"Mode {i} with value {l.real}”)
real_eigs_sorted += [l.real]
loop +=1
if loop == 20: #This is tocontrol the nukmber f eigvalues, not to spit them all
break
Blockquote
#To plot the first eigfunction
lmbda = eigensolver.getEigenpair(0, vr, vi)
x = SpatialCoordinate(domain)
topology1, cell_types1, x = plot.create_vtk_mesh(space)
grid1 = pyvista.UnstructuredGrid(topology1, cell_types1, x)
grid.point_data[“u”] = vr.x.array
warped = grid1.warp_by_scalar(“u”, factor=25)
plotter1 = pyvista.Plotter()
plotter1.add_mesh(warped, show_edges=True, show_scalar_bar=True, scalars=“u”)
plotter1.show()
The error:
raise RuntimeError(“Can only create meshes from continuous or discontinuous Lagrange spaces”)
RuntimeError: Can only create meshes from continuous or discontinuous Lagrange spaces