Skip to content

Commit

Permalink
Added the RegPar for v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Feb 12, 2024
1 parent 69e5bc8 commit 73e3138
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dafoam/mphys/mphys_dafoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ def setup(self):
self.add_input(dvName, distributed=False, shape=nACTDVars, tags=["mphys_coupling"])
elif dvType == "Field": # add field variables
self.add_input(dvName, distributed=True, shape_by_conn=True, tags=["mphys_coupling"])
elif dvType == "RegPar":
nParameters = self.DASolver.solver.getNRegressionParameters()
self.add_input(dvName, distributed=False, shape=nParameters, tags=["mphys_coupling"])
else:
raise AnalysisError("designVarType %s not supported! " % dvType)

Expand Down Expand Up @@ -893,6 +896,19 @@ def apply_linear(self, inputs, outputs, d_inputs, d_outputs, d_residuals, mode):
fieldBar = DASolver.vec2Array(prodVec)
d_inputs[inputName] += fieldBar

elif self.dvType[inputName] == "RegPar":
volCoords = DASolver.vec2Array(DASolver.xvVec)
states = outputs["%s_states" % self.discipline]
parameters = inputs[inputName]
product = np.zeros_like(parameters)
seeds = d_residuals["%s_states" % self.discipline]

DASolver.solverAD.calcdRdRegParTPsiAD(volCoords, states, parameters, seeds, product)

# reduce to make sure all procs get the same product
product = self.comm.allreduce(product, op=MPI.SUM)

d_inputs[inputName] += product
else:
raise AnalysisError("designVarType %s not supported! " % self.dvType[inputName])

Expand Down Expand Up @@ -1205,6 +1221,9 @@ def setup(self):
self.add_input(dvName, distributed=False, shape=nACTDVars, tags=["mphys_coupling"])
elif dvType == "Field": # add field variables
self.add_input(dvName, distributed=True, shape_by_conn=True, tags=["mphys_coupling"])
elif dvType == "RegPar":
nParameters = self.DASolver.solver.getNRegressionParameters()
self.add_input(dvName, distributed=False, shape=nParameters, tags=["mphys_coupling"])
else:
raise AnalysisError("designVarType %s not supported! " % dvType)

Expand Down Expand Up @@ -1405,6 +1424,18 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):
fieldBar = DASolver.vec2Array(dFdField)
d_inputs[inputName] += fieldBar * fBar

elif self.dvType[inputName] == "RegPar":
volCoords = DASolver.vec2Array(DASolver.xvVec)
states = inputs["%s_states" % self.discipline]
parameters = inputs[inputName]
product = np.zeros_like(parameters)

DASolver.solverAD.calcdFdRegParAD(
volCoords, states, parameters, objFuncName.encode(), inputName.encode(), product
)

d_inputs[inputName] += product * fBar

else:
raise AnalysisError("designVarType %s not supported! " % self.dvType[inputName])

Expand Down

0 comments on commit 73e3138

Please sign in to comment.