Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bk/fix-ci-test_dmstag' into vs/p…
Browse files Browse the repository at this point in the history
…etsc-3.22
  • Loading branch information
boriskaus committed Oct 26, 2024
2 parents 302fc3d + fe0debc commit 3edfdff
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 105 deletions.
8 changes: 4 additions & 4 deletions examples/ksp/ex50.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ function main(petsclib; comm = MPI.COMM_WORLD, options...)

opts = if MPI.Comm_size(comm) == 1
(
ksp_monitor = true,
ksp_view = true,
ksp_monitor = false, # set to true for output
ksp_view = false,
da_grid_x = 100,
da_grid_y = 100,
pc_type = "mg",
Expand All @@ -144,8 +144,8 @@ function main(petsclib; comm = MPI.COMM_WORLD, options...)
da_grid_y = 3,
pc_type = "mg",
da_refine = 10,
ksp_monitor = nothing,
ksp_view = nothing,
ksp_monitor = false, # set to true for output
ksp_view = false,
log_view = nothing,
)
end
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if Sys.iswindows()
end

# Do the MPI tests first so we do not have mpi running inside MPI
# XXX: Currently not working on windows, not sure why
# XXX: Currently not working on windows (since we have no PETSc + MPI)
if do_mpi
cmd = `$(mpiexec()) -n 4 $(Base.julia_cmd()) --project dmda.jl`
run(cmd)
Expand All @@ -22,7 +22,7 @@ end
# Examples with the comment
# # INCLUDE IN MPI TEST
# will be run here
# XXX: Currently not working on windows reliably, not sure why
# XXX: Currently not working on windows (since we have no PETSc + MPI)
if do_mpi
include("mpi_examples.jl")
end
Expand Down
205 changes: 108 additions & 97 deletions test/test_dmstag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,13 @@ end
pos3 = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_LEFT,1,0,0,1)
val = PETSc.DMStagVecGetValuesStencil(dm_1D, vec_test, 2, [pos3; pos3])
@test val[2] == 6.0
PETSc.destroy(dm_1D);
#PETSc.destroy(dm_1D);

PETSc.finalize(petsclib)
end
end

# FIXME
# FIXME: part below that is commented segfaults on linux
@testset "DMStag create matrixes" begin
comm = MPI.COMM_WORLD
mpirank = MPI.Comm_rank(comm)
Expand All @@ -319,103 +319,110 @@ end
PETSc.initialize(petsclib)
PetscScalar = PETSc.scalartype(petsclib)
PetscInt = PETSc.inttype(petsclib)
if PetscScalar == Float64 || PetscScalar == Float32

dm_1D = PETSc.DMStagCreate1d(petsclib,comm,PETSc.DM_BOUNDARY_NONE,200,2,2; stag_grid_x=10);
PETSc.setuniformcoordinatesproduct!(dm_1D, (0,), (10,))

A = PETSc.creatematrix(dm_1D); #
PETSc.MatSetOption(A, PETSc.MAT_NEW_NONZERO_ALLOCATION_ERR, false)
@test size(A) == (42,42)
PETSc.assembled(A)

# set some values using normal indices:
A[1,1] = 1.0
A[1,10] = 1.0

pos1 = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_LEFT,1,0,0,1)
pos2 = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_RIGHT,4,0,0,0)
pos = [pos1, pos2]
val1 = PetscScalar.([2222.2, 3.2]);
PETSc.DMStagMatSetValuesStencil(dm_1D, A, pos1, pos1, 11.1, PETSc.INSERT_VALUES)
PETSc.DMStagMatSetValuesStencil(dm_1D, A, 1, [pos2], 2, pos, val1, PETSc.INSERT_VALUES)

PETSc.assemble(A)
@test A[1,10] == 1.0

# Reads a value from the matrix, using the stencil structure
@test PETSc.DMStagMatGetValuesStencil(dm_1D, A, pos1, pos1)== PetscScalar(11.1)
@test PETSc.DMStagMatGetValuesStencil(dm_1D, A, 1, [pos2], 2, pos)==val1

PETSc.destroy(dm_1D);

dofCenter = 1;
dofEdge = 1;
dofVertex = 0
stencilWidth = 1;
dm_2D = PETSc.DMStagCreate2d(petsclib,comm,
PETSc.DM_BOUNDARY_GHOSTED,
PETSc.DM_BOUNDARY_GHOSTED,
10,11,
PETSc.PETSC_DECIDE,PETSc.PETSC_DECIDE,
dofVertex,dofEdge,dofCenter,
PETSc.DMSTAG_STENCIL_BOX,stencilWidth)

vec_test_2D_global = PETSc.createglobalvector(dm_2D)
vec_test_2D_local = PETSc.createlocalvector(dm_2D)

corners = PETSc.getcorners(dm_2D)
ghost_corners = PETSc.getghostcorners(dm_2D)


for ix=corners.lower[1]:corners.upper[1]
for iy=corners.lower[2]:corners.upper[2]
local dof
# DOF at the center point
dof = 0;
posA = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_DOWN,ix,iy,0,dof)
value = PetscScalar(ix+10);
PETSc.DMStagVecSetValuesStencil(dm_2D, vec_test_2D_global, posA, value, PETSc.INSERT_VALUES)

dof = 0;
posB = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_LEFT,ix,iy,0,dof)
value = PetscScalar(33);
PETSc.DMStagVecSetValuesStencil(dm_2D, vec_test_2D_global, posB, value, PETSc.INSERT_VALUES)

dof = 0;
posC = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_ELEMENT,ix,iy,0,dof)
value = PetscScalar(44);
PETSc.DMStagVecSetValuesStencil(dm_2D, vec_test_2D_global, posC, value, PETSc.INSERT_VALUES)
dm_1D = PETSc.DMStagCreate1d(petsclib,comm,PETSc.DM_BOUNDARY_NONE,200,2,2; stag_grid_x=10);
PETSc.setuniformcoordinatesproduct!(dm_1D, (0,), (10,))

A = PETSc.creatematrix(dm_1D); #
PETSc.MatSetOption(A, PETSc.MAT_NEW_NONZERO_ALLOCATION_ERR, false)
@test size(A) == (42,42)


end
# set some values using normal indices:
A[1,1] = 1.0
A[1,10] = 1.0

pos1 = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_LEFT,1,0,0,1)
pos2 = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_RIGHT,4,0,0,0)
pos = [pos1, pos2]
val1 = PetscScalar.([2222.2, 3.2]);
PETSc.DMStagMatSetValuesStencil(dm_1D, A, pos1, pos1, 11.1, PETSc.INSERT_VALUES)
PETSc.DMStagMatSetValuesStencil(dm_1D, A, 1, [pos2], 2, pos, val1, PETSc.INSERT_VALUES)

@test PETSc.assembled(A) == false
PETSc.assemble(A)
@test PETSc.assembled(A) == true
@test A[1,10] == 1.0

# Reads a value from the matrix, using the stencil structure
@test PETSc.DMStagMatGetValuesStencil(dm_1D, A, pos1, pos1)== PetscScalar(11.1)
@test PETSc.DMStagMatGetValuesStencil(dm_1D, A, 1, [pos2], 2, pos)==val1

#PETSc.destroy(A);
PETSc.destroy(dm_1D);

dofCenter = 1;
dofEdge = 1;
dofVertex = 0
stencilWidth = 1;
dm_2D = PETSc.DMStagCreate2d(petsclib,comm,
PETSc.DM_BOUNDARY_GHOSTED,
PETSc.DM_BOUNDARY_GHOSTED,
10,11,
PETSc.PETSC_DECIDE,PETSc.PETSC_DECIDE,
dofVertex,dofEdge,dofCenter,
PETSc.DMSTAG_STENCIL_BOX,stencilWidth)

vec_test_2D_global = PETSc.createglobalvector(dm_2D)
vec_test_2D_local = PETSc.createlocalvector(dm_2D)

corners = PETSc.getcorners(dm_2D)
ghost_corners = PETSc.getghostcorners(dm_2D)

# ----
# FIXME:
# the commented lines below result in a segfault on linux
# To be checked whether this is still the case for the auto-wrapped library
for ix=corners.lower[1]:corners.upper[1]
for iy=corners.lower[2]:corners.upper[2]
local dof
# DOF at the center point
dof = 0;
posA = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_DOWN,ix,iy,0,dof)
value = PetscScalar(ix+10);
#PETSc.DMStagVecSetValuesStencil(dm_2D, vec_test_2D_global, 1, [posA], [value], PETSc.INSERT_VALUES)
dof = 0;
posB = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_LEFT,ix,iy,0,dof)
value = PetscScalar(33);
#PETSc.DMStagVecSetValuesStencil(dm_2D, vec_test_2D_global, posB, value, PETSc.INSERT_VALUES)
dof = 0;
posC = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_ELEMENT,ix,iy,0,dof)
value = PetscScalar(44);
#PETSc.DMStagVecSetValuesStencil(dm_2D, vec_test_2D_global, posC, value, PETSc.INSERT_VALUES)
end
PETSc.assemble(vec_test_2D_global) # assemble global vector
end

PETSc.assemble(vec_test_2D_global) # assemble global vector

# Add the global values to the local values
PETSc.update!(vec_test_2D_local, vec_test_2D_global,PETSc.INSERT_VALUES)
# Add the global values to the local values
PETSc.update!(vec_test_2D_local, vec_test_2D_global,PETSc.INSERT_VALUES)

# retrieve value back from the local array and check that it agrees with global one
dof = 0;
pos = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_DOWN,2,2,0,dof)
@test PETSc.DMStagVecGetValuesStencil(dm_2D, vec_test_2D_local, pos) == 12.0

# retrieve value back from the local array and check that it agrees with global one
dof = 0;
pos = PETSc.DMStagStencil{PetscInt}(PETSc.DMSTAG_DOWN,2,2,0,dof)
# @test PETSc.DMStagVecGetValuesStencil(dm_2D, vec_test_2D_local, pos) == 12.0

# Extract an array that holds all DOF's
X2D_dofs = PETSc.DMStagVecGetArray(dm_2D,vec_test_2D_local) # extract arrays with all DOF (mostly for visualizing)
@test X2D_dofs[4,4,1] PetscScalar(12.0)
@test X2D_dofs[4,4,2] PetscScalar(33.0)
@test X2D_dofs[4,4,3] PetscScalar(44.0)
# # Extract an array that holds all DOF's
X2D_dofs = PETSc.DMStagVecGetArray(dm_2D,vec_test_2D_local) # extract arrays with all DOF (mostly for visualizing)
# @test X2D_dofs[4,4,1] ≈ PetscScalar(12.0)
# @test X2D_dofs[4,4,2] ≈ PetscScalar(33.0)
# @test X2D_dofs[4,4,3] ≈ PetscScalar(44.0)

# Extract an array of a specific DOF (here a face velocity @ the left)
Xarray = PETSc.DMStagGetGhostArrayLocationSlot(dm_2D,vec_test_2D_local, PETSc.DMSTAG_LEFT, 0)
@test sum(X2D_dofs[:,:,2]-Xarray)==0 # check if the local array is identical to the full array
# ----

Xarray .= 111. # Set a value @ a specific location
@test vec_test_2D_local[2] PetscScalar(111) # verify that this is changed in the PETSc Vec
# Extract an array of a specific DOF (here a face velocity @ the left)
Xarray = PETSc.DMStagGetGhostArrayLocationSlot(dm_2D,vec_test_2D_local, PETSc.DMSTAG_LEFT, 0)
@test sum(X2D_dofs[:,:,2]-Xarray)==0 # check if the local array is identical to the full array

Xarray .= 111. # Set a value @ a specific location
@test vec_test_2D_local[2] PetscScalar(111) # verify that this is changed in the PETSc Vec


# cleanup
PETSc.destroy(dm_2D);


end
PETSc.finalize(petsclib)
end
end
Expand Down Expand Up @@ -540,8 +547,8 @@ end
J .= sparse(J_julia[ind,ind]);
end

Base.finalize(x_g)
return sparse(J_julia[ind,ind]), ind
Base.finalize(ptr_x_g)
return sparse(J_julia[ind,ind]), ind
end

# Main part
Expand Down Expand Up @@ -572,31 +579,31 @@ end
S = PETSc.SNES{PetscScalar}(petsclib, comm;
snes_rtol=1e-12,
snes_view=false,
snes_monitor=true,
ksp_view=true,
# pc_type="none",
snes_monitor=false,
ksp_view=false, # set this to true if you want to see output
snes_monitor_true_residual=false,
snes_converged_reason=false);
S.user_ctx = user_ctx;

PETSc.setfunction!(S, FormRes!, f_g)
PETSc.setjacobian!(S, FormJacobian!, PJ, PJ)

# Solve
# Solv
PETSc.solve!(x_g, S);

# check
@test x_g[4] 29.5
@test x_g[11] 0.63797 rtol=1e-4

# cleanup
PETSc.destroy(PJ);
PETSc.destroy(user_ctx.dm);

end
PETSc.finalize(petsclib)
end
end




@testset "DMStag: 2D SNES AD" begin

comm = MPI.COMM_WORLD
Expand Down Expand Up @@ -747,7 +754,7 @@ end

S = PETSc.SNES{PetscScalar}(petsclib, comm;
snes_rtol=1e-12,
snes_monitor=true,
snes_monitor=false, # set to true if you convergence information
pc_type="none",
snes_monitor_true_residual=true,
snes_converged_reason=false);
Expand All @@ -770,6 +777,10 @@ end
#
# -----------------

# cleanup
PETSc.destroy(PJ);
PETSc.destroy(user_ctx.dm);

end
PETSc.finalize(petsclib)

Expand Down
4 changes: 2 additions & 2 deletions test/test_snes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ MPI.Initialized() || MPI.Init()
S = PETSc.SNES{PetscScalar}(petsclib,comm;
ksp_rtol=1e-4,
pc_type="none",
ksp_monitor=true,
snes_monitor=true,
ksp_monitor=false,
snes_monitor=false,
snes_converged_reason=false,
ksp_converged_reason=false)

Expand Down

0 comments on commit 3edfdff

Please sign in to comment.