Skip to content

Commit

Permalink
Fixed a potential issues for all-zero dF seeds (mdolab#667)
Browse files Browse the repository at this point in the history
* Fixed a potential issue for zero fBar.

* Small change to the dF structure.
  • Loading branch information
friedenhe authored Jul 29, 2024
1 parent 6b92e7b commit 21dc6b0
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions dafoam/mphys/mphys_dafoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ def apply_linear(self, inputs, outputs, d_inputs, d_outputs, d_residuals, mode):
d_inputs[inputName] += ACTDBarSub
else:
d_inputs[inputName] += ACTDBar

# compute [dRdHSC]^T*Psi using reverse mode AD
elif self.dvType[inputName] == "HSC":
prodVec = PETSc.Vec().create(self.comm)
Expand Down Expand Up @@ -1351,6 +1351,18 @@ def compute(self, inputs, outputs):
# compute the partial derivatives of functions
def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):

# we first check if all the seeds in d_outputs are zeros. If yes, we return without calculation anything
n_non_zero_seeds = 0
for func_name in d_outputs:
if d_outputs[func_name] != 0.0:
n_non_zero_seeds += 1
if n_non_zero_seeds == 0:
return
elif n_non_zero_seeds > 1:
if self.comm.rank == 0:
print("************* Warning *************")
print("More than one non-zero seed found! ", d_outputs)

DASolver = self.DASolver

# set the runStatus, this is useful when the actuator term is activated
Expand Down Expand Up @@ -1381,7 +1393,8 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):
funcsBar = {}

# assign value to funcsBar. NOTE: we only assign seed if d_outputs has
# non-zero values!
# non-zero values! We assume OM will pass only one non-zero seed here
# therefore, funcsBar should have only one key
if self.funcs is None:
raise AnalysisError("functions not set! Forgot to call mphys_add_funcs?")
else:
Expand All @@ -1394,7 +1407,7 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):

if self.comm.rank == 0:
print("Computing partials for ", list(funcsBar.keys()))

# update the obj func name for solve_linear later
DASolver.setOption("solveLinearObjFuncName", list(funcsBar.keys())[0])
DASolver.updateDAOption()
Expand Down Expand Up @@ -1478,7 +1491,7 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):
d_inputs[inputName] += ACTDBarSub * fBar
else:
d_inputs[inputName] += ACTDBar * fBar

# compute dFdHSC
elif self.dvType[inputName] == "HSC":
dFdHSC = PETSc.Vec().create(self.comm)
Expand Down

0 comments on commit 21dc6b0

Please sign in to comment.