From 73e3138517f28ad1867befc8a80e2a778159b0ba Mon Sep 17 00:00:00 2001 From: Ping He Date: Mon, 12 Feb 2024 16:55:43 -0600 Subject: [PATCH] Added the RegPar for v3. --- dafoam/mphys/mphys_dafoam.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/dafoam/mphys/mphys_dafoam.py b/dafoam/mphys/mphys_dafoam.py index 269d2b62..083fd536 100644 --- a/dafoam/mphys/mphys_dafoam.py +++ b/dafoam/mphys/mphys_dafoam.py @@ -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) @@ -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]) @@ -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) @@ -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])