Issue resolved, stopped using the vector in the struct and used the one in the arguments. Same issue was taking place in the Jacobian function which resulted in error (-8) SNES_DIVERGED_LOCAL_MIN. Adjusting the jacobian function resolved that issue as well.
-----Original Message----- From: petsc-users <petsc-users-boun...@mcs.anl.gov> On Behalf Of petsc-users-requ...@mcs.anl.gov Sent: Sunday, January 7, 2024 3:52 PM To: petsc-users@mcs.anl.gov Subject: petsc-users Digest, Vol 181, Issue 9 ---- External Email: Use caution with attachments, links, or sharing data ---- Send petsc-users mailing list submissions to petsc-users@mcs.anl.gov To subscribe or unsubscribe via the World Wide Web, visit https://lists.mcs.anl.gov/mailman/listinfo/petsc-users or, via email, send a message with subject or body 'help' to petsc-users-requ...@mcs.anl.gov You can reach the person managing the list at petsc-users-ow...@mcs.anl.gov When replying, please edit your Subject line so it is more specific than "Re: Contents of petsc-users digest..." Today's Topics: 1. SNES solve residual vector - Fortran (Khaled Nabil Shar Abdelaziz) ---------------------------------------------------------------------- Message: 1 Date: Sun, 7 Jan 2024 20:51:33 +0000 From: Khaled Nabil Shar Abdelaziz <kabde...@purdue.edu> To: "petsc-users@mcs.anl.gov" <petsc-users@mcs.anl.gov> Subject: [petsc-users] SNES solve residual vector - Fortran Message-ID: <ph7pr22mb363758580c91a9958802a9c6bd...@ph7pr22mb3637.namprd22.prod.outlook.com> Content-Type: text/plain; charset="us-ascii" Hello everyone, I am running into an issue with SNES solver where the residual in the first iteration gets calculated fine, but on the 2nd iteration, even though it is getting calculated locally, it doesn't seem to be "assigned" to the residual vector correctly. The residual vector here is referred to as petsc_PVector which is defined in a type (struct). It seems like the first iteration, it behaves as expected and that petsc_PVector gets updated, 2nd iteration however seems like there are 2 versions of it; one that is being passed into FormFunctionU, and another that exists in the type (struct). This result in the residual on the 2nd iteration to be exactly = 0 and convergence. Any insights, suggestions, or experiences you can share regarding this issue would be immensely appreciated. I'm particularly interested in understanding why the residual vector behaves differently in the second iteration. Best, Khaled The following is a snapshot of how I initialize and use SNES: subroutine set_function_jacobian_to_solver_u(nlU) type(variable_node_u),target,intent(inout)::nlU PetscErrorCode::ierr type(variable_node_part),pointer::var_nd_part integer::ipart ! external FormFunctionU,FormJacobianU do ipart=1,nlU%variable_node%npart var_nd_part=>nlU%variable_node%parts(ipart) PetscCallA(SNESSetFunction(var_nd_part%petsc_snes,var_nd_part%petsc_PVector,FormFunctionU,nlU,ierr)) PetscCallA(SNESSetJacobian(var_nd_part%petsc_snes,var_nd_part%petsc_KMatrix,var_nd_part%petsc_KMatrix,FormJacobianU,nlU,ierr)) PetscCallA(SNESSetFromOptions(var_nd_part%petsc_snes,ierr)) end do var_nd_part=>null() end subroutine set_function_jacobian_to_solver_u subroutine FormFunctionU(petsc_snes,petsc_vector,petsc_PVector,nlU,ierr) type(tSNES)::petsc_snes type(tVec)::petsc_vector type(tVec)::petsc_PVector type(PetscScalar),pointer::vec_ptr_a(:),vec_ptr_b(:) PetscErrorCode::ierr type(variable_node_u),target::nlU type(PetscScalar),pointer::val_petsc_PVector(:) type(parameter),pointer::para_data type(mesh),pointer::mesh_data type(element),pointer::eles(:) integer::k,i,flag,pid PetscInt::its para_data=>nlU%para_data mesh_data=>nlU%mesh_data eles=>nlU%eles call MPI_Comm_rank(MPI_COMM_WORLD, pid, ierr) call update_data_by_petsc_vector(nlU%variable_node,petsc_vector) call set_petsc_PVector_to_zero(nlU%variable_node) call update_petsc_PVector(nlU%variable_node) flag = 0 do k=1,mesh_data%nel call get_element_value(nlU%data_e, eles(k)%nnd_e, mesh_data%elements(:,k), nlU%nvar_total, nlU%data) call cal_element_ResidualU(nlU%PVector_e, eles(k), nlU%data_e, para_data) call set_petsc_PVector_values_by_element(nlU%variable_node, nlU%PVector_e, eles(k)%nnd_e, mesh_data%elements(:,k)) end do call update_petsc_PVector(nlU%variable_node) para_data=>null() mesh_data=>null() eles=>null() return end subroutine FormFunctionU call SNESSolve(nlU%variable_node%petsc_snes,PETSC_NULL_VEC, nlU%variable_node%petsc_vector,error_petsc) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240107/683fa2d8/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ petsc-users mailing list petsc-users@mcs.anl.gov https://lists.mcs.anl.gov/mailman/listinfo/petsc-users ------------------------------ End of petsc-users Digest, Vol 181, Issue 9 *******************************************