How to change the Hessian matrices and gradient vectors in dolfinx

Hi, everyone. I would like to ask a technical question on how to extract and assemble Hessian matrices and gradient vectors on my own. Now I am using an algorithm requiring modified Hessian matrices and gradient vectors in Newton’s method. Specifically, mesh and degree of freedom of FEM are fixed, but some values of nodes on the Hessian matrix and gradient vector will be changed. So, how to implement this operation in Dolfinx? Thank you.

You differentiate the residual with respect to the unknown, compile the form and call assembe_matrix on it once you have modified coefficients?
Could you provide some context to what you are currently doing, and what you are not able to do?

Thank you. I am trying to reproduce and test the Neural operator element method, which embeds neural operators into FEM by modifying the Hessian matrix and gradient vector in Newton’s method. See details of Appendix C of following paper.

Ouyang, W., Shin, Y., Liu, S. W., & Lu, L. (2025). Neural-operator element method: Efficient and scalable finite element method enabled by reusable neural operators. arXiv preprint arXiv:2506.18427.

We now commonly use dolfinx to write our FEM program, so I want to research it on dolfinx. It is obvious that this method requires to modify the Hessian matrices and gradient vectors at each Newton iteration, that is, to assemble the automatic differentiation results of the neural network into these matrices and vectors. I’m not sure how to operate the iterative process in dolfinx or should PETSc be used? Or, may I achieve similar operations by modifying the Jacobian matrix?

This depends on what version of DOLFINx you use.
I would recommend the nightly version, where we interface with snes, in a way that makes it easy for users to create custom assembly operators: dolfinx/python/test/unit/fem/test_petsc_nonlinear_assembler.py at 44fd625a852e37058fc4a0c8da28b4fcef44e2d4 · FEniCS/dolfinx · GitHub
you can drop-in replace either of those functions with your own implementations/modifications:
dolfinx/python/dolfinx/fem/petsc.py at main · FEniCS/dolfinx · GitHub

Thanks. I am currently using version v0.9 downloaded from conda: conda install -c conda-forge fenics-dolfinx mpich pyvista It seems like I should try the nightly version on Docker.

Btw, I would like to ask about when the v0.10 version will be officially released?

Hopefully soon. We are trying to wrap up some new features to include in the new release.

A question. If I want to change values of residual vectors and Jacobian matrix of some nodes in each Newton iteration, how shell I complete it? Due to residual and Jacobian are objects but not matrixes in the FEniCSx, I am not sure how to modify values of specific nodes on the Jacobian matrix that has been assembled.

If you are using the ‘petsc` backend for matrices, you can use the setvalueslocal command:

or for multiple values at the time: MatSetValuesLocal — PETSc 3.23.6 documentation

Vectors are easier, as you can access the `array_w’ which is a numpy array petsc4py.PETSc.Vec — Python 3.23.6 documentation