Jason Sum
Subscriber

Dear Akram,

I would like to update you my latest attemp on adjoint solving with boundary condition interchange, grateful to have your guidance!

This is my approach: 
1. setting the outlet face flow rate as the observable in adjoint setup

2. initialising and running the adjoint solver (currently struggling adjoint residual convergence too)

3. after some settings in design tool, setting up two operation conditions in gradient-based optimiser (1. forward flow, 2. reverse flow) with one input parameter "flow-direction" (0 for forward flow, 1 for reverse flow). Then set up the targets of the observable for the two conditions respectively. (As you guided, the taget percentages of the two conditions should be experimented)

4. Creating a macro in Execute Command under gradient-based optimiser, where the following script saved in the macro and run for each design iteration. I tested the code without the IF condition and it suceeded to run both flow cases automatically without errors, but it is not going well when adding the IF condition and being called as an execute command.

(if (eqv? (get-input-parameter-value 'flow-direction) 0)
   (begin
 
       ;; Scheme script for setting boundary conditions in Fluent - Forward
       (display "Setting boundary conditions...\n")
 
       ;; Set pressure inlet for zone 6 with a total pressure of 0 Pa
       (ti-menu-load-string "define/boundary-conditions/zone-type 6 pressure-inlet")
      (ti-menu-load-string "define/boundary-conditions pressure-inlet 6 yes no 0 no 0 no yes no no yes 5 10 q")
 
     ;; Set pressure outlet for zone 7 with a gauge pressure of -50 Pa
     (ti-menu-load-string "define/boundary-conditions/zone-type 7 pressure-outlet")
    (ti-menu-load-string "define/boundary-conditions pressure-outlet 7 yes no -50 no yes no no yes 5 10 yes no no q")
 
    (display "Boundary conditions set successfully.\n")
 
    ;; Initialize and solve forward flow
    (display "Simulating forward flow...\n")
    (ti-menu-load-string "solve/initialize/hyb-initialization ok")
    (ti-menu-load-string "solve/iterate 1500")  ;; Adjust iteration count as necessary
 
 
    ;; Record the flow rate at BC ID 6 (forward flow)
   (display "Recording flow rate for forward flow at BC ID 6...\n")
   (define forward-flow-rate
       (ti-menu-load-string "report/surface-integrals/mass-flow-rate 1 () yes forward-flow"))
 
   (display "Forward flow rate at BC ID 6: ")
   (display forward-flow-rate)
   (display "\n")
  )
 
;;----------------------------------------------------------------------------------
 (begin
    ;; Scheme script for setting boundary conditions in Fluent - Reverse
    (display "Setting boundary conditions...\n")
 
    ;; Set pressure inlet for zone 6 with a total pressure of -50 Pa
   (ti-menu-load-string "define/boundary-conditions/zone-type 6 pressure-outlet")
   (ti-menu-load-string "define/boundary-conditions pressure-outlet 6 yes no -50 no yes no no yes 5 10 yes no no q")
 
   ;; Set pressure outlet for zone 7 with a gauge pressure of 0 Pa
   (ti-menu-load-string "define/boundary-conditions/zone-type 7 pressure-inlet")
  (ti-menu-load-string "define/boundary-conditions pressure-inlet 7 yes no -0 no 0 no yes no no yes 5 10 q")
 
   (display "Boundary conditions set successfully.\n")
 
   ;; Initialize and solve reverse flow
   (display "Simulating forward flow...\n")
  (ti-menu-load-string "solve/initialize/hyb-initialization ok")
  (ti-menu-load-string "solve/iterate 1500")  ;; Adjust iteration count as necessary
 
 
   ;; Record the flow rate at BC ID 6 (reverse flow)
   (display "Recording flow rate for reverse flow at BC ID 6...\n")
   (define reverse-flow-rate
   (ti-menu-load-string "report/surface-integrals/mass-flow-rate 1 () yes reverse-flow"))
 
   (display "Reverse flow rate at BC ID 6: ")
   (display reverse-flow-rate)
   (display "\n")
   )
)
;@before-flow