Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/underworld3/cython/petsc_generic_snes_solvers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2777,15 +2777,15 @@ 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 = []
for i, fn in enumerate(conds):
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")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -8752,7 +8752,7 @@ class SNES_Stokes_SaddlePt(SolverBaseClass):
<const PetscInt *> &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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/underworld3/cython/petsc_types.pxd
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/underworld3/utilities/_jitextension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <petscsystypes.h>
#include <math.h>

// Adding missing function implementation
Expand Down
78 changes: 78 additions & 0 deletions tests/test_0764_petsc_index_boundary_conditions.py
Original file line number Diff line number Diff line change
@@ -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)
Loading