diff --git a/src/underworld3/cython/petsc_generic_snes_solvers.pyx b/src/underworld3/cython/petsc_generic_snes_solvers.pyx index d4227e83d..ab69a1d01 100644 --- a/src/underworld3/cython/petsc_generic_snes_solvers.pyx +++ b/src/underworld3/cython/petsc_generic_snes_solvers.pyx @@ -2777,7 +2777,7 @@ class SolverBaseClass(uw_object): "with None / sympy.oo entries in 'conds' instead, e.g. " "conds=(None, 5, 1.2)", DeprecationWarning, stacklevel=3) - components = np.array(components, dtype=np.int32, ndmin=1) + components = np.array(components, dtype=PETSc.IntType, ndmin=1) elif components is None: cpts_list = [] @@ -2785,7 +2785,7 @@ class SolverBaseClass(uw_object): if fn != sympy.oo and fn != -sympy.oo: cpts_list.append(i) - components = np.array(cpts_list, dtype=np.int32, ndmin=1) + components = np.array(cpts_list, dtype=PETSc.IntType, ndmin=1) else: raise TypeError("Unsupported BC 'components' argument") @@ -4009,8 +4009,8 @@ class SNES_Scalar(SolverBaseClass): self.dm.createDS() # set functions - cdef int ind=1 - cdef int [::1] comps_view # for numpy memory view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view # for numpy memory view cdef DM cdm = self.dm cdef DS ds = self.dm.getDS() cdef PtrContainer ext = self.compiled_extensions @@ -4271,8 +4271,8 @@ class SNES_Scalar(SolverBaseClass): return # set functions - cdef int ind=1 - cdef int [::1] comps_view # for numpy memory view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view # for numpy memory view cdef DM cdm = self.dm cdef DS ds = self.dm.getDS() cdef PtrContainer ext = self.compiled_extensions @@ -4906,7 +4906,7 @@ class SNES_Vector(SolverBaseClass): ]) import numpy as np - components = np.arange(cdim, dtype=np.int32) + components = np.arange(cdim, dtype=PETSc.IntType) self.natural_bcs.append(BC( 0, components, fn_f, fn_F, None, @@ -4970,8 +4970,8 @@ class SNES_Vector(SolverBaseClass): ## This part is done once on the solver dm ... not required every time we update the functions ... ## the values of the natural bcs can be updated - cdef int ind=1 - cdef int [::1] comps_view # for numpy memory view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view # for numpy memory view cdef DM cdm = self.dm for index,bc in enumerate(self.natural_bcs): @@ -5295,8 +5295,8 @@ class SNES_Vector(SolverBaseClass): return # set functions - cdef int ind=1 - cdef int [::1] comps_view # for numpy memory view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view # for numpy memory view cdef DM cdm = self.dm cdef DS ds = self.dm.getDS() cdef PtrContainer ext = self.compiled_extensions @@ -5735,8 +5735,8 @@ class SNES_MultiComponent(SolverBaseClass): self.dm.createDS() - cdef int ind=1 - cdef int [::1] comps_view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view cdef DM cdm = self.dm for index, bc in enumerate(self.natural_bcs): @@ -6022,8 +6022,8 @@ class SNES_MultiComponent(SolverBaseClass): print(f"SNES_MultiComponent ({self.name}): SNES solver does not need to be rebuilt", flush=True) return - cdef int ind=1 - cdef int [::1] comps_view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view cdef DM cdm = self.dm cdef DS ds = self.dm.getDS() cdef PtrContainer ext = self.compiled_extensions @@ -7151,7 +7151,7 @@ class SNES_Stokes_SaddlePt(SolverBaseClass): ]) import numpy as np - components = np.arange(dim, dtype=np.int32) + components = np.arange(dim, dtype=PETSc.IntType) self.natural_bcs.append(BC( 0, components, fn_f, fn_F, fn_p, @@ -8682,8 +8682,8 @@ class SNES_Stokes_SaddlePt(SolverBaseClass): ## This part is done once on the solver dm ... not required every time we update the functions ... ## the values of the natural bcs can be updated - cdef int ind=1 - cdef int [::1] comps_view # for numpy memory view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view # for numpy memory view cdef DM cdm = self.dm for index,bc in enumerate(self.natural_bcs): @@ -8729,15 +8729,15 @@ class SNES_Stokes_SaddlePt(SolverBaseClass): # u-row traction h·n and the uh Jacobian) and once for the multiplier # field (carries the h-row constraint n·u−g and the hu Jacobian). # Guarded: no-op for ordinary Stokes. - cdef int [::1] cbc_comps_view - cdef int [::1] cbc_hcomps_view + cdef PetscInt [::1] cbc_comps_view + cdef PetscInt [::1] cbc_hcomps_view for cbc in self._block_constraint_bcs: cbc_boundary = cbc.boundary cbc_value = mesh.boundaries[cbc_boundary].value ind = cbc_value cbc_fid_h = cbc.lam._solver_field_id - cbc_comps = np.arange(mesh.dim, dtype=np.int32) + cbc_comps = np.arange(mesh.dim, dtype=PETSc.IntType) cbc_comps_view = cbc_comps cbc.petsc_id_u = PetscDSAddBoundary_UW(cdm.dm, 6, @@ -8752,7 +8752,7 @@ class SNES_Stokes_SaddlePt(SolverBaseClass): &ind, NULL, ) - cbc_hcomps = np.array([0], dtype=np.int32) + cbc_hcomps = np.array([0], dtype=PETSc.IntType) cbc_hcomps_view = cbc_hcomps cbc.petsc_id_h = PetscDSAddBoundary_UW(cdm.dm, 6, @@ -8964,11 +8964,11 @@ class SNES_Stokes_SaddlePt(SolverBaseClass): fidx |= set(extra_field[(p, f)]) if fidx: fidx = sorted(fidx) - Snew.setFieldConstraintIndices(p, f, np.array(fidx, dtype=np.int32)) + Snew.setFieldConstraintIndices(p, f, np.array(fidx, dtype=PETSc.IntType)) point_local.extend(field_offset + i for i in fidx) field_offset += fdof if point_local: - Snew.setConstraintIndices(p, np.array(sorted(point_local), dtype=np.int32)) + Snew.setConstraintIndices(p, np.array(sorted(point_local), dtype=PETSc.IntType)) dm.setLocalSection(Snew) # Force the global-section rebuild (and fail fast if malformed); the @@ -8987,8 +8987,8 @@ class SNES_Stokes_SaddlePt(SolverBaseClass): return # set functions - cdef int ind=1 - cdef int [::1] comps_view # for numpy memory view + cdef PetscInt ind=1 + cdef PetscInt [::1] comps_view # for numpy memory view cdef DM cdm = self.dm cdef DS ds = self.dm.getDS() cdef PtrContainer ext = self.compiled_extensions diff --git a/src/underworld3/cython/petsc_types.pxd b/src/underworld3/cython/petsc_types.pxd index 07ce18876..af86c5f3a 100644 --- a/src/underworld3/cython/petsc_types.pxd +++ b/src/underworld3/cython/petsc_types.pxd @@ -1,7 +1,9 @@ # Underworld Cython declarations to use PetscDS functionality # The following aren't available via petsc4py - Oct2021 -ctypedef int PetscInt +# PETSc selects this width in petscconf.h; a local C int alias breaks int64 builds. +cdef extern from "petscsystypes.h": + ctypedef int PetscInt ctypedef double PetscReal ctypedef double PetscScalar ctypedef int PetscErrorCode diff --git a/src/underworld3/utilities/_jitextension.py b/src/underworld3/utilities/_jitextension.py index 6387b33e3..d0e7a1bb1 100644 --- a/src/underworld3/utilities/_jitextension.py +++ b/src/underworld3/utilities/_jitextension.py @@ -1455,10 +1455,7 @@ def _patch_coords(expr): # Create header top content. h_str = """ -typedef int PetscInt; -typedef double PetscReal; -typedef double PetscScalar; -typedef int PetscBool; +#include #include // Adding missing function implementation diff --git a/tests/test_0764_petsc_index_boundary_conditions.py b/tests/test_0764_petsc_index_boundary_conditions.py new file mode 100644 index 000000000..2903962d3 --- /dev/null +++ b/tests/test_0764_petsc_index_boundary_conditions.py @@ -0,0 +1,78 @@ +"""Boundary registration must preserve PETSc's configured integer width. + +The exact Couette solution is u=(y, 0), p=constant on the unit square. +Exercise both inferred and legacy explicit component lists, including [0, 1], +which a 64-bit PETSc build misread as 4294967296 from a 32-bit buffer. +""" + +import numpy as np +import pytest +from petsc4py import PETSc + +import underworld3 as uw + +pytestmark = pytest.mark.level_1 + + +@pytest.mark.tier_a +@pytest.mark.parametrize("explicit_components", [False, True]) +@pytest.mark.parametrize("dim", [2, 3]) +def test_couette_boundary_components_use_petsc_integer_width(explicit_components, dim): + mesh = uw.meshing.StructuredQuadBox(elementRes=(4,) * dim, qdegree=3) + velocity = uw.discretisation.MeshVariable("index_velocity", mesh, dim, degree=2) + pressure = uw.discretisation.MeshVariable("index_pressure", mesh, 1, degree=1) + stokes = uw.systems.Stokes(mesh, velocityField=velocity, pressureField=pressure) + stokes.constitutive_model = uw.constitutive_models.ViscousFlowModel + stokes.constitutive_model.Parameters.viscosity = 1 + stokes.bodyforce = (0,) * dim + y = mesh.N.y + conds = (y,) + (0,) * (dim - 1) + boundaries = ["Top", "Bottom", "Left", "Right"] + if dim == 3: + boundaries += ["Front", "Back"] + for boundary in boundaries: + if explicit_components: + with pytest.warns(DeprecationWarning, match="components"): + stokes.add_dirichlet_bc(conds, boundary, tuple(range(dim))) + else: + stokes.add_dirichlet_bc(conds, boundary) + + for bc in stokes.essential_bcs: + assert bc.components.dtype == np.dtype(PETSc.IntType) + np.testing.assert_array_equal(bc.components, np.arange(dim)) + + stokes.petsc_use_pressure_nullspace = True + # The exact pressure is constant. Its Schur RHS is round-off (~6e-14) + # in parallel, so a purely relative subsolve asks for unattainable accuracy. + stokes.petsc_options["fieldsplit_pressure_ksp_atol"] = 1e-12 + stokes.tolerance = 1e-9 + stokes.solve() + assert stokes.snes.getConvergedReason() > 0 + coords = velocity.coords + expected = np.zeros((len(coords), dim)) + expected[:, 0] = coords[:, 1] + np.testing.assert_allclose(velocity.array[:, 0, :], expected, rtol=1e-7, atol=1e-8) + + +@pytest.mark.tier_a +def test_couette_natural_boundary_components_use_petsc_integer_width(): + """Unit shear: natural BC terms enter the residual with minus the traction.""" + mesh = uw.meshing.StructuredQuadBox(elementRes=(4, 4), qdegree=3) + velocity = uw.discretisation.MeshVariable("traction_velocity", mesh, 2, degree=2) + pressure = uw.discretisation.MeshVariable("traction_pressure", mesh, 1, degree=1) + stokes = uw.systems.Stokes(mesh, velocityField=velocity, pressureField=pressure) + stokes.constitutive_model = uw.constitutive_models.ViscousFlowModel + stokes.constitutive_model.Parameters.viscosity = 1 + stokes.bodyforce = (0, 0) + stokes.add_dirichlet_bc((mesh.N.y, 0), "Top") + stokes.add_dirichlet_bc((mesh.N.y, 0), "Bottom") + stokes.add_natural_bc((0, 1), "Left") + stokes.add_natural_bc((0, -1), "Right") + for bc in stokes.natural_bcs: + assert bc.components.dtype == np.dtype(PETSc.IntType) + np.testing.assert_array_equal(bc.components, [0, 1]) + stokes.tolerance = 1e-9 + stokes.solve() + assert stokes.snes.getConvergedReason() > 0 + expected = np.column_stack((velocity.coords[:, 1], np.zeros(len(velocity.coords)))) + np.testing.assert_allclose(velocity.array[:, 0, :], expected, rtol=1e-7, atol=1e-8)