[petsc-users] Different precision from MatAssembly/MatView

Matthew Knepley knepley at gmail.com
Sun Mar 15 11:12:01 CDT 2026


On Sun, Mar 15, 2026 at 11:19 AM Noam T. via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> A bit more information:
>
> Looking at the solution of the system of equations, knowing the exact RHS
> vector:
> ---
> [-0.00197, 0.00203, -0.00197]
> ---
>
> SNES gives the solution:
> ---
>  3.316262462871E-03, -4.189244774965E-03, 2.468317119413E-03
> ---
>
> which is indeed (closer to) the solution for the jacobian shown in MatView
> (the "lower precision" one). The "exact" solution would be:
> ---
> 3.282607248309093b-3, -4.241572005990172b-3,, 2.425030835360701b-3
> ---
>
> which is already different form the second decimal digit.
>

Are you using an exact solution method, like LU?

  Thanks,

     Matt


>
> ***
> Requesting a low precision to have SNES do a few iterations, shows that
> the built jacobian seems to deviate more and more form the computed one
> after some iterations, e.g.:
>
> Computed (printed from in-code):
> ---
>  0.536488494840   0.066474939660    0.198813816284
>  0.066474939660   0.529482312614   -0.002835445071
>  0.198813816284  -0.002835445071    0.530589055263
> ---
>
> MatView:
> ---
> row 0: (0, 0.538491)   (1, 0.0662019)    (2, 0.198808)
> row 1: (0, 0.0662019)  (1, 0.526981)     (2, -0.00257554)
> row 2: (0, 0.198808)   (1, -0.00257554)  (2, 0.529883)
> ---
>
> Only one digit is correct/the same in entries [1,2], [2,1] and [2,2].
>
> Thank you.
> Noam
>
>
> On Sunday, March 15th, 2026 at 3:29 PM, Noam T. <dontbugthedevs at proton.me>
> wrote:
>
> Hello,
>
> Looking at the assembly of the Jacobian using MatView, I noticed values
> were somewhat different from the computed ones in the jacobian function
> (DMSNESSetJacobian). For the test, I used a single Q1 element, with 4
> quadrature points, so the exact Jacobian matrix can be computed
> analytically.
>
> In the jacobian function, after looping through all quadrature points,
> printing the matrix to stdout shows:
>
> ----
>  0.531360000000   0.066670000000   0.197333333327
>  0.066670000000   0.535360000000  -0.003333333327
>  0.197333333327  -0.003333333327   0.527413333333
> ----
>
> (showing only the 3 free DoFs). The 2x2 submatrix in [0:1] has exactly
> four/five nonzero digits, as shown above. The rest of the elements have a
> periodic 3 ending (a couple of digits at the end are off, but that's fine).
>
> This same matrix is the one added to the global jacobian during the
> quadrature loop, with "DMPlexMatSetClosure". After looping, calling
> "MatAssemblyBegin/End", then MatView:
>
> ----
> Mat Object: 1 MPI process
>   type: seqaij
> row 0: (0, 0.531387)   (1, 0.0666733)   (2, 0.197338)
> row 1: (0, 0.0666733)  (1, 0.535387)    (2, -0.0033385)
> row 2: (0, 0.197338)   (1, -0.0033385)  (2, 0.52744)
> ----
>
> Values are close, but definitely not the same as computed. Are these
> values from "MatView" the ones actually stored in the matrix? It seems
> there is some precision loss in the process.
>
> Interestingly, computing the jacobian through finite differences, shows
> the same result for the "hand-coded" jacobian, whereas the finite
> differences ones is the "exact" one (same as first one above):
>
> ----
>   ||J - Jfd||_F/||J||_F = 4.90736e-05, ||J - Jfd||_F = 4.74265e-05
>   Hand-coded Jacobian ----------
> Mat Object: 1 MPI process
>   type: seqaij
> row 0: (0, 0.531387)  (1, 0.0666733)  (2, 0.197338)
> row 1: (0, 0.0666733)  (1, 0.535387)  (2, -0.0033385)
> row 2: (0, 0.197338)  (1, -0.0033385)  (2, 0.52744)
>   Finite difference Jacobian ----------
> Mat Object: 1 MPI process
>   type: seqaij
> row 0: (0, 0.53136)  (1, 0.06667)  (2, 0.197333)
> row 1: (0, 0.06667)  (1, 0.53536)  (2, -0.00333333)
> row 2: (0, 0.197333)  (1, -0.00333333)  (2, 0.527413)
>   Hand-coded minus finite-difference Jacobian with tolerance 1e-05
> ----------
> Mat Object: 1 MPI process
>   type: seqaij
> row 0: (0, 2.66554e-05)
> row 1: (1, 2.66554e-05)
> row 2: (2, 2.6558e-05)
>  ----
>
> The main code is in Fortran, using double precision variables, in case it
> is relevant.
>
> Thank you.
>
> Noam
>
>
>
>

-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!aG7692I0cAJ6_5ASPxacsW2kfd1x_zlBDB_pNqHAWEF8Xp_sDoXgg6uSoo0rZxD62Wd5jE29HQ8JqdWSNidk$  <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!aG7692I0cAJ6_5ASPxacsW2kfd1x_zlBDB_pNqHAWEF8Xp_sDoXgg6uSoo0rZxD62Wd5jE29HQ8JqYaRBd2v$ >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20260315/b66b4e87/attachment-0001.html>


More information about the petsc-users mailing list