You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Computing a onesided integral over an interior facet with consistent orientation# Enabled by: https://github.com/FEniCS/dolfinx/pull/2269# Copyright 2023 Jørgen S. Dokken# SPDX: MITimportdolfinximportnumpyasnpimportuflfrommpi4pyimportMPImesh=dolfinx.mesh.create_unit_square(
MPI.COMM_WORLD, 10, 10, ghost_mode=dolfinx.mesh.GhostMode.shared_facet)
# Create connectivties required for defining integration entitiestdim=mesh.topology.dimfdim=tdim-1mesh.topology.create_entities(fdim)
mesh.topology.create_connectivity(fdim, tdim)
mesh.topology.create_connectivity(tdim, fdim)
# Get number of cells on processcell_map=mesh.topology.index_map(tdim)
num_cells=cell_map.size_local+cell_map.num_ghosts# Create markers for each size of the interfacecell_values=np.ones(num_cells, dtype=np.int32)
cell_values[dolfinx.mesh.locate_entities(
mesh, tdim, lambdax: x[0] <=0.5+1e-13)] =2ct=dolfinx.mesh.meshtags(mesh, tdim, np.arange(
num_cells, dtype=np.int32), cell_values)
facet_map=mesh.topology.index_map(fdim)
num_facets=facet_map.size_local+facet_map.num_ghosts# Create facet markersfacet_values=np.ones(num_facets, dtype=np.int32)
facet_values[dolfinx.mesh.locate_entities(
mesh, fdim, lambdax: np.isclose(x[0], 0.5))] =2ft=dolfinx.mesh.meshtags(mesh, fdim, np.arange(
num_facets, dtype=np.int32), facet_values)
# Give a set of facets marked with a value (in this case 2), get a consistent orientation for an interior integralfacets_to_integrate=ft.find(2)
f_to_c=mesh.topology.connectivity(fdim, tdim)
c_to_f=mesh.topology.connectivity(tdim, fdim)
# Compute integration entities for a single facet of a cell.# Each facet is represented as a tuple (cell_index, local_facet_index), where cell_index is local to process# local_facet_index is the local indexing of a facet for a given cellintegration_entities= []
fori, facetinenumerate(facets_to_integrate):
# Only loop over facets owned by the process to avoid duplicate integrationiffacet>=facet_map.size_local:
continue# Find cells connected to facetcells=f_to_c.links(facet)
# Get value of cellsmarked_cells=ct.values[cells]
# Get the cell marked with 2correct_cell=np.flatnonzero(marked_cells==2)
assertlen(correct_cell) ==1# Get local index of facetlocal_facets=c_to_f.links(cells[correct_cell[0]])
local_index=np.flatnonzero(local_facets==facet)
assertlen(local_index) ==1# Append integration entitiesintegration_entities.append(cells[correct_cell[0]])
integration_entities.append(local_index[0])
# Create custom integration measure for one-sided integralsbreakpoint()
ds=ufl.Measure("ds", domain=mesh, subdomain_data=[
(8, np.asarray(integration_entities, dtype=np.int32))])
n=ufl.FacetNormal(mesh)
x=ufl.SpatialCoordinate(mesh)
# Exact integral is [y/2**2]_0^1= 1/2L=ufl.dot(ufl.as_vector((x[1], 0)), n)*ds(8)
L_compiled=dolfinx.fem.form(L)
print(
f"Correct integral: {mesh.comm.allreduce(dolfinx.fem.assemble_scalar(L_compiled), op=MPI.SUM)}")
# Create reference implementation where we use a restricted two-sided integral with no notion of orientationdS=ufl.Measure("dS", domain=mesh, subdomain_data=ft, subdomain_id=2)
n=ufl.FacetNormal(mesh)
x=ufl.SpatialCoordinate(mesh)
L2=ufl.dot(ufl.as_vector((x[1], 0)), n("+"))*dSL2_compiled=dolfinx.fem.form(L2)
print(
f"Wrong integral: {mesh.comm.allreduce(dolfinx.fem.assemble_scalar(L2_compiled), op=MPI.SUM)}")
I am trying this out on dolfinx=0.8.0 and running into the error (8, np.asarray(integration_entities, dtype=np.int32))]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (20,) + inhomogeneous part.
I believe that the line integration_entities.append(local_index) should instead be integration_entities.append(local_index[0])
Source: https://fenicsproject.discourse.group/t/wrong-facetnormal-vector-on-internal-boundaries/12887/2?u=dokken
The text was updated successfully, but these errors were encountered: