Skip to content

Commit

Permalink
Fix an issue in setThermal (#555)
Browse files Browse the repository at this point in the history
* Added avg to the reg print range.

* setThermal assign BCs for both orig and AD fields.

* Fixed a build bug for setThermal.

* Updated aerothermal ref.
  • Loading branch information
friedenhe authored Jan 6, 2024
1 parent b2cbb36 commit d7073e1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 50 deletions.
28 changes: 21 additions & 7 deletions dafoam/mphys/mphys_dafoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def setup(self):

def add_dvgeo(self, DVGeo):
self.DVGeo = DVGeo

def add_dvcon(self, DVCon):
self.DVCon = DVCon

Expand Down Expand Up @@ -724,10 +724,10 @@ def solve_nonlinear(self, inputs, outputs):
if couplingInfo["aerothermal"]["active"]:
if self.discipline == "aero":
T_convect = inputs["T_convect"]
DASolver.solver.setThermal(T_convect)
DASolver.setThermal(T_convect)
elif self.discipline == "thermal":
q_conduct = inputs["q_conduct"]
DASolver.solver.setThermal(q_conduct)
DASolver.setThermal(q_conduct)
else:
raise AnalysisError("discipline not valid!")

Expand Down Expand Up @@ -948,13 +948,17 @@ def solve_linear(self, d_outputs, d_residuals, mode):

if DASolver.getOption("writeDeformedFFDs"):
if self.DVGeo is None:
raise RuntimeError("writeDeformedFFDs is set but no DVGeo object found! Please call add_dvgeo in the run script!")
raise RuntimeError(
"writeDeformedFFDs is set but no DVGeo object found! Please call add_dvgeo in the run script!"
)
else:
self.DVGeo.writeTecplot("deformedFFDs_%d.dat" % self.solution_counter)

if DASolver.getOption("writeDeformedConstraints"):
if self.DVCon is None:
raise RuntimeError("writeDeformedConstraints is set but no DVCon object found! Please call add_dvcon in the run script!")
raise RuntimeError(
"writeDeformedConstraints is set but no DVCon object found! Please call add_dvcon in the run script!"
)
else:
self.DVCon.writeTecplot("deformedConstraints_%d.dat" % self.solution_counter)

Expand Down Expand Up @@ -2122,7 +2126,16 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):
prodVec = PETSc.Vec().createSeq(2, bsize=1, comm=PETSc.COMM_SELF)
prodVec.zeroEntries()
DASolver.solverAD.calcdFvSourcedInputsTPsiAD(
propName.encode(), "targetForce".encode(), aVec, tVec, rVec, fVec, cVec, xvVec, sBarVec, prodVec
propName.encode(),
"targetForce".encode(),
aVec,
tVec,
rVec,
fVec,
cVec,
xvVec,
sBarVec,
prodVec,
)
fBar = DASolver.vec2ArraySeq(prodVec)
fBar = self.comm.allreduce(fBar, op=MPI.SUM)
Expand All @@ -2148,6 +2161,7 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):
# vBar = self.comm.allreduce(vBar, op=MPI.SUM)
d_inputs["%s_vol_coords" % self.discipline] += vBar


class DAFoamPropNodes(ExplicitComponent):
"""
Component that computes propeller aero-node locations that link with structural nodes in aerostructural cases.
Expand Down
14 changes: 14 additions & 0 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -4083,6 +4083,20 @@ def _initOption(self, name, value):
"Received data type is %-47s" % (name, self.defaultOptions[name][0], type(value))
)

def setThermal(self, thermalArray):
"""
Update the values in thermalArray to OpenFOAM's temperature boundary conditions
Parameters
----------
thermalArray: array
thermal array that contains the boundary values for OpenFOAM's temperature variable
"""

self.solver.setThermal(thermalArray)
if self.getOption("useAD")["mode"] in ["forward", "reverse"]:
self.solverAD.setThermal(thermalArray)

def setRegressionParameter(self, idx, val):
"""
Update the regression parameters
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DARegression/DARegression.C
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void DARegression::compute()
{
forAll(inputNames_, idxI)
{
Info << inputNames_[idxI] << " Max: " << gMax(inputFields[idxI]) << " Min: " << gMin(inputFields[idxI]) << endl;
Info << inputNames_[idxI] << " Max: " << gMax(inputFields[idxI]) << " Min: " << gMin(inputFields[idxI]) << " Avg: " << gAverage(inputFields[idxI]) << endl;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/adjoint/DASolver/DASolver.C
Original file line number Diff line number Diff line change
Expand Up @@ -9729,6 +9729,9 @@ label DASolver::validateStates()

if (fail > 0)
{
Info << "*************************************** Warning! ***************************************" << endl;
Info << "Invalid values found. Return primal failure and reset the states to their initial values" << endl;
Info << "*************************************** Warning! ***************************************" << endl;
this->resetStateVals();
return 1;
}
Expand Down
15 changes: 9 additions & 6 deletions src/pyDASolvers/DASolvers.H
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,15 @@ public:
/// set the thermal (temperature or heat flux) BC to the conjugate heat transfer patches
void setThermal(double* thermal)
{
#if !defined(CODI_AD_REVERSE) && !defined(CODI_AD_FORWARD)
DASolverPtr_->setThermal(thermal);
return;
#endif
FatalErrorIn("setThermal") << "should not call this func in AD mode!"
<< abort(FatalError);
label nCouplingFaces = this->getNCouplingFaces();
scalar* thermalArray = new scalar[nCouplingFaces * 2];
for (label i = 0; i < nCouplingFaces * 2; i++)
{
thermalArray[i] = thermal[i];
}

DASolverPtr_->setThermal(thermalArray);
delete[] thermalArray;
}

/// get the number of regression model parameters
Expand Down
72 changes: 36 additions & 36 deletions tests/refs/DAFoam_Test_MphysAeroThermalRef.txt
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
Dictionary Key: HFH
Dictionary Key: shape
@value -7.124682631833473 0.0001 1e-06
@value -7.12556981822929 0.0001 1e-06
@value 5.600158171212374 0.0001 1e-06
@value 5.605863775493755 0.0001 1e-06
@value -6.570371718813171 0.0001 1e-06
@value -6.559580675093131 0.0001 1e-06
@value 6.516591553493792 0.0001 1e-06
@value 6.520303855242699 0.0001 1e-06
@value -6.831936009900216 0.0001 1e-06
@value -6.826660381288622 0.0001 1e-06
@value 6.571461067286109 0.0001 1e-06
@value 6.570517451542607 0.0001 1e-06
@value -7.000156220333093 0.0001 1e-06
@value -7.001072836471439 0.0001 1e-06
@value 5.482350187612933 0.0001 1e-06
@value 5.48781525099412 0.0001 1e-06
@value -6.470176215085266 0.0001 1e-06
@value -6.459657677895588 0.0001 1e-06
@value 6.415881526368159 0.0001 1e-06
@value 6.419627549825165 0.0001 1e-06
@value -6.746443174920007 0.0001 1e-06
@value -6.741459479206545 0.0001 1e-06
@value 6.49083054426032 0.0001 1e-06
@value 6.489772648699112 0.0001 1e-06
Dictionary Key: HF_INNER
Dictionary Key: shape
@value -4.202340391804171 0.0001 1e-06
@value -4.217222331798023 0.0001 1e-06
@value 4.108313626017049 0.0001 1e-06
@value 4.103166721507856 0.0001 1e-06
@value -3.888703855636824 0.0001 1e-06
@value -3.88282390193669 0.0001 1e-06
@value 4.003064101409218 0.0001 1e-06
@value 3.999717349137935 0.0001 1e-06
@value -3.969197926485898 0.0001 1e-06
@value -3.970585275800125 0.0001 1e-06
@value 3.880686952026748 0.0001 1e-06
@value 3.882122492604914 0.0001 1e-06
@value -4.202340280841435 0.0001 1e-06
@value -4.217222098170075 0.0001 1e-06
@value 4.108313358443013 0.0001 1e-06
@value 4.103166598874045 0.0001 1e-06
@value -3.888704134129874 0.0001 1e-06
@value -3.882823787782875 0.0001 1e-06
@value 4.003064000059666 0.0001 1e-06
@value 3.999717604532754 0.0001 1e-06
@value -3.969197942069573 0.0001 1e-06
@value -3.9705856795767 0.0001 1e-06
@value 3.880687326277144 0.0001 1e-06
@value 3.882122590063406 0.0001 1e-06
Dictionary Key: PL
Dictionary Key: shape
@value 5.924459816504415 0.0001 1e-06
@value 5.924265114827 0.0001 1e-06
@value -5.92505299313147 0.0001 1e-06
@value -5.925104425350948 0.0001 1e-06
@value 5.54653354711235 0.0001 1e-06
@value 5.546631305756967 0.0001 1e-06
@value -5.543722048587866 0.0001 1e-06
@value -5.544238225521984 0.0001 1e-06
@value 5.597474265160635 0.0001 1e-06
@value 5.597439769934142 0.0001 1e-06
@value -5.596109312774072 0.0001 1e-06
@value -5.596437334965213 0.0001 1e-06
@value 5.92445981653273 0.0001 1e-06
@value 5.924265114818993 0.0001 1e-06
@value -5.925052993041589 0.0001 1e-06
@value -5.925104425327937 0.0001 1e-06
@value 5.546533547206108 0.0001 1e-06
@value 5.546631305918375 0.0001 1e-06
@value -5.543722048598633 0.0001 1e-06
@value -5.544238225551973 0.0001 1e-06
@value 5.597474265173706 0.0001 1e-06
@value 5.597439769922604 0.0001 1e-06
@value -5.596109312799514 0.0001 1e-06
@value -5.596437334986424 0.0001 1e-06

0 comments on commit d7073e1

Please sign in to comment.