[petsc-users] Fortran, KSPMonitorSet, and KSPMonitorTrueResidual

Randall Mackie rlmackie862 at gmail.com
Mon Jun 23 13:46:52 CDT 2025


In previous versions of PETSc we use to be able to call KSPMonitorTrueResidual from within our custom KSPMonitor, using an approach that is now commented out in the example found at https://urldefense.us/v3/__https://petsc.org/release/src/ksp/ksp/tutorials/ex2f.F90.html__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8twfJqLvXA$ :

!
214:  <>!     Cannot also use the default KSP <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSP/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tx3EWnatA$ > monitor routine showing how it may be used from Fortran
215:  <>!     since the Fortran compiler thinks the calling arguments are different in the two cases
216:  <>!
217:  <>!        PetscCallA <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Sys/PetscCallA/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzA4vLgOQ$ >(PetscViewerAndFormatCreate <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PetscViewerAndFormatCreate/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tykmBKBfg$ >(PETSC_VIEWER_STDOUT_WORLD <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PETSC_VIEWER_STDOUT_WORLD/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tw_EZkgfA$ >,PETSC_VIEWER_DEFAULT <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PetscViewerFormat/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tyZ_k-1Uw$ >,vf,ierr))
218:  <>!        PetscCallA <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Sys/PetscCallA/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzA4vLgOQ$ >(KSPMonitorSet <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSPMonitorSet/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tx6T8FJsA$ >(ksp,KSPMonitorResidual <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSPMonitorResidual/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8txdiRZPSg$ >,vf,PetscViewerAndFormatDestroy <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Viewer/PetscViewerAndFormatDestroy/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzN4PpBPA$ >,ierr))

Instead, that example uses:

210:  <>      if (flg) then
211:  <>        vzero = 0
212:  <>        PetscCallA <https://urldefense.us/v3/__https://petsc.org/release/manualpages/Sys/PetscCallA/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzA4vLgOQ$ >(KSPMonitorSet <https://urldefense.us/v3/__https://petsc.org/release/manualpages/KSP/KSPMonitorSet/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tx6T8FJsA$ >(ksp,MyKSPMonitor,vzero,PETSC_NULL_FUNCTION,ierr))
213:  <>!
Regardless of which of these approaches I try, I cannot use KSPMonitorTrueResidual in the MyKSPMonitor routine.
I get the following error:

[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
[0]PETSC ERROR: Null argument, when expecting valid pointer
[0]PETSC ERROR: Null Pointer: Parameter # 4
[0]PETSC ERROR: See https://urldefense.us/v3/__https://petsc.org/release/faq/__;!!G_uCfscf7eWS!ft8A0uxwHvTcBa5FwETVQYcImMgoOOueBeZT-7zP9jlaLFCDZ6ApRkBFUgoUzMRWCLbyU1FcxTgFoYMm8tzxt44McA$  for trouble shooting.
[0]PETSC ERROR: PETSc Release Version 3.23.3, May 30, 2025
[0]PETSC ERROR: ./test with 2 MPI process(es) and PETSC_ARCH linux-gfortran-complex-debug on rmackie-VirtualBox-2024 by rmackie Mon Jun 23 11:34:04 2025
[0]PETSC ERROR: Configure options: --with-clean=1 --with-scalar-type=complex --with-debugging=1 --with-fortran=1 --download-mpich=1
[0]PETSC ERROR: #1 KSPMonitorTrueResidual() at /home/rmackie/PETSc/petsc-3.23.3/src/ksp/ksp/interface/iterativ.c:400
[0]PETSC ERROR: #2 test.F90:303


I attach a slightly modified version of the example that demonstrates this behavior.


Thanks for the help,

Randy

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250623/b399c0ac/attachment-0002.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.F90
Type: application/octet-stream
Size: 13409 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250623/b399c0ac/attachment-0001.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250623/b399c0ac/attachment-0003.html>


More information about the petsc-users mailing list