Skip to content

Commit

Permalink
Added Poisson test
Browse files Browse the repository at this point in the history
Signed-off-by: Umberto Zerbinati <[email protected]>
  • Loading branch information
Umberto Zerbinati committed Oct 28, 2023
1 parent b3d4221 commit af179a8
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions tests/test_fenicsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,62 @@ def test_square_netgen():
'''
Testing FEniCSx interface with Netgen generating a square mesh
'''
from mpi4py import MPI
import ngsPETSc.utils.fenicsx as fx
from dolfinx.io import XDMFFile
try:
pass
from mpi4py import MPI
import ngsPETSc.utils.fenicsx as ngfx
from dolfinx.io import XDMFFile
except ImportError:
pytest.skip(msg="DOLFINx unavailable, skipping FENICSx test")

from netgen.geom2d import SplineGeometry
geo = SplineGeometry()
geo.AddRectangle((0,0),(1,1))
geoModel = fx.GeometricModel(geo, MPI.COMM_WORLD)
domain =geoModel .model_to_mesh(hmax=0.1)
geoModel = ngfx.GeometricModel(geo, MPI.COMM_WORLD)
domain =geoModel.model_to_mesh(hmax=0.1)
with XDMFFile(domain.comm, "XDMF/mesh.xdmf", "w") as xdmf:
xdmf.write_mesh(domain)

def test_poisson_netgen():
'''
Testing FEniCSx interface with Netgen generating a square mesh
'''
try:
import numpy as np
import ufl
from dolfinx import fem, mesh
from dolfinx.fem.petsc import LinearProblem
from ufl import dx, grad, inner
from mpi4py import MPI
from petsc4py.PETSc import ScalarType
import ngsPETSc.utils.fenicsx as ngfx
except ImportError:
pytest.skip(msg="DOLFINx unavailable, skipping FENICSx test")

from netgen.geom2d import SplineGeometry
geo = SplineGeometry()
geo.AddRectangle((0,0),(np.pi,np.pi))
geoModel = ngfx.GeometricModel(geo, MPI.COMM_WORLD)
msh =geoModel.model_to_mesh(hmax=0.1)
V = fem.FunctionSpace(msh, ("Lagrange", 2))
facetsLR = mesh.locate_entities_boundary(msh, dim=(msh.topology.dim - 1),
marker=lambda x: np.logical_or(np.isclose(x[0], 0.0),
np.isclose(x[0], np.pi)))
facetsTB = mesh.locate_entities_boundary(msh, dim=(msh.topology.dim - 1),
marker=lambda x: np.logical_or(np.isclose(x[1], 0.0),
np.isclose(x[1], np.pi)))
facets = np.append(facetsLR,facetsTB)
dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)
bc = fem.dirichletbc(value=ScalarType(0), dofs=dofs, V=V)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = ufl.exp(ufl.sin(x[0])*ufl.sin(x[1]))
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx
problem = LinearProblem(a, L, bcs=[bc],
petsc_options={"ksp_type": "cg", "pc_type": "qr"})
problem.solve()

if __name__ == "__main__":
test_square_netgen()
test_poisson_netgen()

0 comments on commit af179a8

Please sign in to comment.