CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > SU2

Extracting Jacobian Matrices from SU2

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree4Likes
  • 2 Post By R.K
  • 1 Post By R.K
  • 1 Post By R.K

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 18, 2024, 13:37
Default Extracting Jacobian Matrices from SU2
  #1
R.K
New Member
 
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3
R.K is on a distinguished road
Hi SU2 community,

I am trying to perform adjoint optimisation using SU2 as a black box for the simulations and python (JAX) for derivatives. I have already found the derivative of the objective function with respect to the flow field (∂J/∂U) using JAX's autodiff. To form the adjoint problem I need the derivatives of residual with respect to the flow field and design variables (∂R/∂U and ∂R/∂x).

I was reading the source code and believe they are stored (at least ∂R/∂U, not sure about ∂R/∂x) in the CSysSolve_b.cpp and CSysMatrix.cpp codes. Is there a way to extract them?

Thank you.
RK
wglao and Tongtong1919 like this.
R.K is offline   Reply With Quote

Old   July 18, 2024, 15:47
Default
  #2
Member
 
Josh Kelly
Join Date: Dec 2018
Posts: 57
Rep Power: 8
joshkellyjak is on a distinguished road
I am not sure why you would use JAX to calculate the sensitivties when SU2 has adjoint capabilities already, which you would have to run to calculate dR/dx. If you have located where they are stored I think you would have to implement a method to extract them, is there a specific reason why you want to use JAX to assemble your gradients?
joshkellyjak is offline   Reply With Quote

Old   July 19, 2024, 09:22
Default
  #3
R.K
New Member
 
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3
R.K is on a distinguished road
I should have been clearer in my original post. My objective function is to reduce the momentum deficit in the boundary layer. To evaluate it I need to extract the boundary layer profiles over my geometry. Currently I am using the VTk library to extract them. I don't know if theres a way to extract them in SU2, I am open to suggestions if there is. While SU2 has adjoint capabilities, I am not sure how to write my custom objective function into the source code. There is a way of writing a custom objective function in the config file, but that works for variables calculated within the code (from what I understand). Since the input of my objective function are the boundary layer profiles, I dont think they can be incorporated or evaluated through the config file. As for using JAX, its auto-diff capabilities provide a greater flexibility in computing the gradient (of nearly any function).

For extracting the Jacobians, do I need to write a function in the source code of CSysMatrix.cpp? Additionally the Jacobian is calculated for each solver (eg in CAdjNSSolver.cpp). Do I need to change anything here? My knowledge in C++ is very limited.
Tongtong1919 likes this.

Last edited by R.K; July 19, 2024 at 09:24. Reason: Clarity
R.K is offline   Reply With Quote

Old   July 19, 2024, 09:42
Default
  #4
Member
 
Josh Kelly
Join Date: Dec 2018
Posts: 57
Rep Power: 8
joshkellyjak is on a distinguished road
By the sounds of it you will probably have to modify from the source code. I think your best bet is to join the developer meetings to ask your question in more detail - I think some figures would be beneficial to understand what you are trying to do. SU2 already contains AD tools and this is what is used to calculate the jacobians you want to extract. I think you would need to include your objective function in this calculation to solve your adjoint linear system for dR/dU.
joshkellyjak is offline   Reply With Quote

Old   March 25, 2025, 14:51
Default
  #5
New Member
 
Wesley Lao
Join Date: Mar 2023
Posts: 3
Rep Power: 4
wglao is on a distinguished road
Hello R.K.

I am trying to do something similar. Did you every find a way to extract the Jacobian or at least some way to calculate a jvp?

Wesley
wglao is offline   Reply With Quote

Old   March 26, 2025, 05:33
Default
  #6
R.K
New Member
 
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3
R.K is on a distinguished road
Hi Wesley,

I managed to solve my problem by coupling the flow gradients (∂J/∂U and ∂J/∂X) with the code, solving for the adjoint equations to get the adjoint sensitivities and then performing the optimisation. This allows for the Jacobians to be called implicitly during the calculations, rather than storing these large (sparse) matrices somewhere, then using them externally - which can be very expensive.

Can you elaborate on what you are trying to do? There may be a similar solution.
R.K is offline   Reply With Quote

Old   March 26, 2025, 17:20
Default
  #7
New Member
 
Wesley Lao
Join Date: Mar 2023
Posts: 3
Rep Power: 4
wglao is on a distinguished road
Hi R.K.

I am trying to get some functions \phi := (\partial J / \partial U)^{-1} f where f is a linear operator taking the solution U to some quantity. I believe all I need is to define an objective L in the adjoint source code such that the resulting adjoint equation gives \phi as the adjoint solution with SU2_CFD_AD.

Wesley
wglao is offline   Reply With Quote

Old   March 27, 2025, 06:25
Default
  #8
R.K
New Member
 
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3
R.K is on a distinguished road
Hi Wesley,

It looks like you are trying to do the same thing as I was. What is your objective function? If it is definable in the source code, it would be best to do that. You can also define the function in the config by setting:
```OBJECTIVE_FUNCTION= CUSTOM_OBJFUNC
CUSTOM_OBJFUNC= your_function
```
there is a tutorial on this (https://github.com/su2code/SU2/blob/..._flatplate.cfg) and a presentation (https://www.youtube.com/watch?v=T6MxTnm2v-Q&t=729s)

For my case, it was too miscellaneous to be defined in the config or in the source code, hence I evaluated the objective function and its gradients wrt flow and mesh outside (using python-jax/ pytorch) and coupled them with the source code to perform the optimisation. I can share the code if your function is complex, like mine was. This method is slightly long winded, but works for any function, so long as you can code it.

Last edited by R.K; March 27, 2025 at 06:25. Reason: url link
R.K is offline   Reply With Quote

Old   March 27, 2025, 13:11
Default
  #9
New Member
 
Wesley Lao
Join Date: Mar 2023
Posts: 3
Rep Power: 4
wglao is on a distinguished road
Hi R.K.

I have to make it work for any arbitrary solution-to-quantity functional, but I will try defining the objectives using the config to test for now. Thanks for your help!

Wesley
wglao is offline   Reply With Quote

Old   March 27, 2025, 15:09
Default
  #10
R.K
New Member
 
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3
R.K is on a distinguished road
The only reason I recommend writing the function in the config is that it saves a lot of time and effort, not just coding the function, but setting up the optimisation as well, since SU2 will evaluate all the function and its gradients internally using its own AD. There are limitations as to what you can define in the config, hence I had to take things outside SU2.

this is the amended code I wrote to solve my problem and should solve yours as well (https://github.com/Bot-Enigma-0/SU2). It is stable for v8.0.1. Please only use the master/main branch the others are different things. For the optimisation you can use FADO (https://github.com/su2code/FADO), which is also what i used. tutorial 4 is related to CFD and should help set up the problem for you.

To make it work you need to use the multizone solver (no need to change anything in the mesh, just the config), set OBJECTIVE_FUNCTION= CUSTOM_OBJFUNC and CUSTOM_OBJFUNC= 0, save the flow gradients as del_J__del_U_autodiff.csv and del_J__del_X_autodiff.csv for the mesh gradients. run the adjoint (SU2_CFD_AD) as normal to set the adjoint sensitivities.

I've been meaning to make it more user friendly and add testcases/tutorials, but have been sidetracked. From the testing I have done it agrees very well with the in-built functions.
Tongtong1919 likes this.
R.K is offline   Reply With Quote

Reply

Tags
adjoint variable, jacobian

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Jacobian jyotir SU2 0 July 7, 2023 01:19
Introducing SU2 International Developers Society (IDS) fpalacios SU2 News & Announcements 1 June 17, 2019 22:38
SU2 crashed when "Initialize Jacobian structure" falltime SU2 2 October 20, 2014 20:01
Jacobian Coefficient matrices for the viscous flux Andy Main CFD Forum 8 February 25, 2004 23:53
Jacobian Coefficient matrices for the viscous flux Andy Main CFD Forum 0 February 19, 2004 13:59


All times are GMT -4. The time now is 14:14.