|
[Sponsors] |
![]() |
![]() |
#1 |
New Member
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3 ![]() |
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 |
|
![]() |
![]() |
![]() |
![]() |
#2 |
Member
Josh Kelly
Join Date: Dec 2018
Posts: 57
Rep Power: 8 ![]() |
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?
|
|
![]() |
![]() |
![]() |
![]() |
#3 |
New Member
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3 ![]() |
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. Last edited by R.K; July 19, 2024 at 09:24. Reason: Clarity |
|
![]() |
![]() |
![]() |
![]() |
#4 |
Member
Josh Kelly
Join Date: Dec 2018
Posts: 57
Rep Power: 8 ![]() |
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.
|
|
![]() |
![]() |
![]() |
![]() |
#5 |
New Member
Wesley Lao
Join Date: Mar 2023
Posts: 3
Rep Power: 4 ![]() |
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 |
|
![]() |
![]() |
![]() |
![]() |
#6 |
New Member
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3 ![]() |
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. |
|
![]() |
![]() |
![]() |
![]() |
#7 |
New Member
Wesley Lao
Join Date: Mar 2023
Posts: 3
Rep Power: 4 ![]() |
||
![]() |
![]() |
![]() |
![]() |
#8 |
New Member
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3 ![]() |
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 |
|
![]() |
![]() |
![]() |
![]() |
#9 |
New Member
Wesley Lao
Join Date: Mar 2023
Posts: 3
Rep Power: 4 ![]() |
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 |
|
![]() |
![]() |
![]() |
![]() |
#10 |
New Member
rutvik
Join Date: Mar 2024
Posts: 19
Rep Power: 3 ![]() |
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. |
|
![]() |
![]() |
![]() |
Tags |
adjoint variable, jacobian |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
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 |