From 123452493532d93e87e710cdf04581d9c8024fbf Mon Sep 17 00:00:00 2001 From: Ping He Date: Tue, 24 Sep 2024 13:59:29 -0500 Subject: [PATCH 1/5] Update codecov.yml --- .github/workflows/codecov.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 3a821081..09d39cc8 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -34,8 +34,7 @@ jobs: args: 'mphys_comp' steps: - - uses: actions/checkout@v3 - - uses: codecov/codecov-action@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v2 with: python-version: 3.8 @@ -70,6 +69,10 @@ jobs: docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/src/adjoint && cp -r ../include ./Make/linux*/DASolver/ && cp -r ../include ./Make/linux*/" docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/src/adjointAD && cp -r ../include ./Make/linux*/DASolver/ && cp -r ../include ./Make/linux*/" docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && lcov --capture --directory . --output-file coverage.info && echo dafoamuser | sudo -S cp -r coverage.info ${{env.DOCKER_MOUNT_DIR}}/" - curl -Os https://uploader.codecov.io/latest/linux/codecov - chmod +x codecov - ./codecov + - name: Upload reports to CodeCov + uses: codecov/codecov-action@v4 + with: + fail_ci_if_error: true + files: ./coverage.info,./coverage.xml + token: ${{secrets.CODECOV_TOKEN}} + verbose: true From 39692e8458bf41ccb127cecb13d74653bf0cc18c Mon Sep 17 00:00:00 2001 From: Ping He Date: Wed, 9 Oct 2024 15:45:00 -0500 Subject: [PATCH 2/5] Added an incompressible alphatWallFunction. (#692) --- src/adjoint/Make/files_Incompressible | 1 + ...phatWallFunctionIncompFvPatchScalarField.C | 157 +++++++++++++++++ ...phatWallFunctionIncompFvPatchScalarField.H | 162 ++++++++++++++++++ 3 files changed, 320 insertions(+) create mode 100755 src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.C create mode 100755 src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.H diff --git a/src/adjoint/Make/files_Incompressible b/src/adjoint/Make/files_Incompressible index 18b93b41..f2916074 100755 --- a/src/adjoint/Make/files_Incompressible +++ b/src/adjoint/Make/files_Incompressible @@ -107,6 +107,7 @@ boundaryConditions/multiFreqVector/multiFreqVectorFvPatchField.C boundaryConditions/nutUSpaldingWallFunctionDF/nutUSpaldingWallFunctionFvPatchScalarFieldDF.C boundaryConditions/varyingVelocity/varyingVelocityFvPatchVectorField.C boundaryConditions/varyingVelocityInletOutlet/varyingVelocityInletOutletFvPatchVectorField.C +boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.C models/dummyTurbulenceModel/makeDummyTurbulenceModelIncompressible.C models/SpalartAllmarasFv3/makeSpalartAllmarasFv3Incompressible.C models/SpalartAllmarasFv3FieldInversion/makeSpalartAllmarasFv3FieldInversionIncompressible.C diff --git a/src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.C b/src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.C new file mode 100755 index 00000000..58f0eedb --- /dev/null +++ b/src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.C @@ -0,0 +1,157 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "alphatWallFunctionIncompFvPatchScalarField.H" +#include "fvPatchFieldMapper.H" +#include "volFields.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace incompressible +{ +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +alphatWallFunctionIncompressibleFvPatchScalarField:: +alphatWallFunctionIncompressibleFvPatchScalarField +( + const fvPatch& p, + const DimensionedField& iF +) +: + fixedValueFvPatchScalarField(p, iF), + Prt_(0.85) +{ +} + + +alphatWallFunctionIncompressibleFvPatchScalarField:: +alphatWallFunctionIncompressibleFvPatchScalarField +( + const alphatWallFunctionIncompressibleFvPatchScalarField& ptf, + const fvPatch& p, + const DimensionedField& iF, + const fvPatchFieldMapper& mapper +) +: + fixedValueFvPatchScalarField(ptf, p, iF, mapper), + Prt_(ptf.Prt_) +{ +} + + +alphatWallFunctionIncompressibleFvPatchScalarField:: +alphatWallFunctionIncompressibleFvPatchScalarField +( + const fvPatch& p, + const DimensionedField& iF, + const dictionary& dict +) +: + fixedValueFvPatchScalarField(p, iF, dict), + Prt_(dict.get("Prt")) // force read to avoid ambiguity +{ +} + + +alphatWallFunctionIncompressibleFvPatchScalarField:: +alphatWallFunctionIncompressibleFvPatchScalarField +( + const alphatWallFunctionIncompressibleFvPatchScalarField& wfpsf +) +: + fixedValueFvPatchScalarField(wfpsf), + Prt_(wfpsf.Prt_) +{ +} + + +alphatWallFunctionIncompressibleFvPatchScalarField:: +alphatWallFunctionIncompressibleFvPatchScalarField +( + const alphatWallFunctionIncompressibleFvPatchScalarField& wfpsf, + const DimensionedField& iF +) +: + fixedValueFvPatchScalarField(wfpsf, iF), + Prt_(wfpsf.Prt_) +{ +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void alphatWallFunctionIncompressibleFvPatchScalarField::updateCoeffs() +{ + if (updated()) + { + return; + } + + const label patchi = patch().index(); + + // Retrieve turbulence properties from model + + const turbulenceModel& turbModel = db().lookupObject + ( + IOobject::groupName + ( + turbulenceModel::propertiesName, + internalField().group() + ) + ); + + const tmp tnutw = turbModel.nut(patchi); + + operator==(tnutw/Prt_); + + fixedValueFvPatchField::updateCoeffs(); +} + + +void alphatWallFunctionIncompressibleFvPatchScalarField::write(Ostream& os) const +{ + fvPatchField::write(os); + os.writeEntry("Prt", Prt_); + writeEntry("value", os); +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +makePatchTypeField +( + fvPatchScalarField, + alphatWallFunctionIncompressibleFvPatchScalarField +); + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace incompressible +} // End namespace Foam + +// ************************************************************************* // diff --git a/src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.H b/src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.H new file mode 100755 index 00000000..1793415c --- /dev/null +++ b/src/adjoint/boundaryConditions/alphatWallFunctionIncomp/alphatWallFunctionIncompFvPatchScalarField.H @@ -0,0 +1,162 @@ +/*---------------------------------------------------------------------------*\ + + DAFoam : Discrete Adjoint with OpenFOAM + Version : v3 + + This file is modified from OpenFOAM's source code + src/TurbulenceModels/compressible/turbulentFluidThermoModels/derivedFvPatchFields/ + wallFunctions/alphatWallFunctionIncompressibles/alphatWallFunctionIncompressible + + The turbulent thermal diffusivity calculated using: + + \f[ + \alpha_t = \frac{\nu_t}{Pr_t} + \f] + + OpenFOAM: The Open Source CFD Toolbox + + Copyright (C): 2011-2016 OpenFOAM Foundation + + OpenFOAM License: + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + + Description: + Fixed traction boundary condition for the standard linear elastic, + fixed coefficient displacement equation. + +\*---------------------------------------------------------------------------*/ + +#ifndef alphatWallFunctionIncompressibleFvPatchScalarField_H +#define alphatWallFunctionIncompressibleFvPatchScalarField_H + +#include "fixedValueFvPatchFields.H" +#include "turbulenceModel.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace incompressible +{ + +/*---------------------------------------------------------------------------*\ + Class alphatWallFunctionIncompressibleFvPatchScalarField Declaration +\*---------------------------------------------------------------------------*/ + +class alphatWallFunctionIncompressibleFvPatchScalarField +: + public fixedValueFvPatchScalarField +{ +protected: + + // Protected data + + //- Turbulent Prandtl number + scalar Prt_; + +public: + + //- Runtime type information + TypeName("incompressible::alphatWallFunction"); + + + // Constructors + + //- Construct from patch and internal field + alphatWallFunctionIncompressibleFvPatchScalarField + ( + const fvPatch&, + const DimensionedField& + ); + + //- Construct from patch, internal field and dictionary + alphatWallFunctionIncompressibleFvPatchScalarField + ( + const fvPatch&, + const DimensionedField&, + const dictionary& + ); + + //- Construct by mapping given + // alphatWallFunctionIncompressibleFvPatchScalarField + // onto a new patch + alphatWallFunctionIncompressibleFvPatchScalarField + ( + const alphatWallFunctionIncompressibleFvPatchScalarField&, + const fvPatch&, + const DimensionedField&, + const fvPatchFieldMapper& + ); + + //- Construct as copy + alphatWallFunctionIncompressibleFvPatchScalarField + ( + const alphatWallFunctionIncompressibleFvPatchScalarField& + ); + + //- Construct and return a clone + virtual tmp clone() const + { + return tmp + ( + new alphatWallFunctionIncompressibleFvPatchScalarField(*this) + ); + } + + //- Construct as copy setting internal field reference + alphatWallFunctionIncompressibleFvPatchScalarField + ( + const alphatWallFunctionIncompressibleFvPatchScalarField&, + const DimensionedField& + ); + + //- Construct and return a clone setting internal field reference + virtual tmp clone + ( + const DimensionedField& iF + ) const + { + return tmp + ( + new alphatWallFunctionIncompressibleFvPatchScalarField(*this, iF) + ); + } + + + // Member functions + + // Evaluation functions + + //- Update the coefficients associated with the patch field + virtual void updateCoeffs(); + + + // I-O + + //- Write + virtual void write(Ostream&) const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace incompressible +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // From bd386fd0eaa09b46a1d6ea1960545c896147c2c3 Mon Sep 17 00:00:00 2001 From: Ping He Date: Thu, 10 Oct 2024 10:28:34 -0500 Subject: [PATCH 3/5] Fixed an issue of un-initialized thermal coupling vars --- dafoam/mphys/mphys_dafoam.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dafoam/mphys/mphys_dafoam.py b/dafoam/mphys/mphys_dafoam.py index ddbcaf6d..12ff6e5c 100644 --- a/dafoam/mphys/mphys_dafoam.py +++ b/dafoam/mphys/mphys_dafoam.py @@ -632,9 +632,17 @@ def setup(self): # NOTE: here we create two duplicated surface center coords, so the size is nFaces * 2 # one is for transferring near wall temperature, the other is for transferring k/d coefficients if self.discipline == "aero": - self.add_input("T_convect", distributed=True, shape=2 * nFaces, tags=["mphys_coupling"]) + # we need to set initial values for T_convect + T_convect0 = np.zeros(2 * nFaces) + for n in range(nFaces): + T_convect0[n] = 300.0 + self.add_input("T_convect", distributed=True, val=T_convect0, tags=["mphys_coupling"]) if self.discipline == "thermal": - self.add_input("q_conduct", distributed=True, shape=2 * nFaces, tags=["mphys_coupling"]) + # we need to set initial values for q_conduct + q_conduct0 = np.zeros(2 * nFaces) + for n in range(nFaces): + q_conduct0[n] = 300.0 + self.add_input("q_conduct", distributed=True, val=q_conduct0, tags=["mphys_coupling"]) # now loop over the design variable keys to determine which other variables we need to add shapeVarAdded = False From 17bcaf0b2a16ec5f6c942a699aaf7625e7e2d394 Mon Sep 17 00:00:00 2001 From: Chris Psenica <122938605+ChrisPsenica@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:34:22 -0600 Subject: [PATCH 4/5] Adding in flag for calculating the heat flux per unit area or for total surface (#704) --- src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C | 50 +++++++++++++++---- src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.H | 3 ++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C b/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C index 80affbf2..8f529834 100755 --- a/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C +++ b/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C @@ -74,11 +74,23 @@ DAObjFuncWallHeatFlux::DAObjFuncWallHeatFlux( "calculated") #endif { + // Assign type, this is common for all objectives objFuncDict_.readEntry("type", objFuncType_); objFuncDict_.readEntry("scale", scale_); + // Heat flux can be calculated by either per unit area or over entire surface. Default value is byUnitArea + if (objFuncDict_.found("scheme")) + { + objFuncDict_.readEntry("scheme", calcMode_); + } + else + { + calcMode_ = "byUnitArea"; + } + + #ifdef CompressibleFlow // setup the connectivity for heat flux, this is needed in Foam::DAJacCondFdW @@ -162,17 +174,20 @@ void DAObjFuncWallHeatFlux::calcObjFunc( objFuncValue: the sum of objective, reduced across all processors and scaled by "scale" */ - // always calculate the area of all the heat flux patches - areaSum_ = 0.0; - forAll(objFuncFaceSources, idxI) + // if calcMode is per unit area then calculate the area of all the heat flux patches + if (calcMode_ == "byUnitArea") { - const label& objFuncFaceI = objFuncFaceSources[idxI]; - label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces; - const label patchI = daIndex_.bFacePatchI[bFaceI]; - const label faceI = daIndex_.bFaceFaceI[bFaceI]; - areaSum_ += mesh_.magSf().boundaryField()[patchI][faceI]; + areaSum_ = 0.0; + forAll(objFuncFaceSources, idxI) + { + const label& objFuncFaceI = objFuncFaceSources[idxI]; + label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces; + const label patchI = daIndex_.bFacePatchI[bFaceI]; + const label faceI = daIndex_.bFaceFaceI[bFaceI]; + areaSum_ += mesh_.magSf().boundaryField()[patchI][faceI]; + } + reduce(areaSum_, sumOp()); } - reduce(areaSum_, sumOp()); // initialize faceValues to zero forAll(objFuncFaceValues, idxI) @@ -238,7 +253,22 @@ void DAObjFuncWallHeatFlux::calcObjFunc( const label faceI = daIndex_.bFaceFaceI[bFaceI]; scalar area = mesh_.magSf().boundaryField()[patchI][faceI]; - objFuncFaceValues[idxI] = scale_ * wallHeatFluxBf[patchI][faceI] * area / areaSum_; + + if (calcMode_ == "byUnitArea") + { + objFuncFaceValues[idxI] = scale_ * wallHeatFluxBf[patchI][faceI] * area / areaSum_; + } + else if (calcMode_ == "total") + { + objFuncFaceValues[idxI] = scale_ * wallHeatFluxBf[patchI][faceI] * area; + } + else + { + FatalErrorIn(" ") << "mode for " + << objFuncName_ << " " << objFuncPart_ << " not valid!" + << "Options: byUnitArea (default value), total." + << abort(FatalError); + } objFuncValue += objFuncFaceValues[idxI]; } diff --git a/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.H b/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.H index 53d208ef..de33ab1b 100755 --- a/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.H +++ b/src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.H @@ -50,6 +50,9 @@ protected: /// the area of all heat flux patches scalar areaSum_ = -9999.0; + /// if calculating flux per unit area or total, which mode to use + word calcMode_; + public: TypeName("wallHeatFlux"); // Constructors From 08422e96df249433b607455f451d193d21f1b91e Mon Sep 17 00:00:00 2001 From: Ping He Date: Wed, 1 Jan 2025 22:14:12 -0600 Subject: [PATCH 5/5] Used mpicc to auto-detec include and lib paths. --- src/adjoint/Make/options_Compressible | 6 ++---- src/adjoint/Make/options_Incompressible | 6 ++---- src/adjoint/Make/options_Solid | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/adjoint/Make/options_Compressible b/src/adjoint/Make/options_Compressible index 7bda4623..d0372547 100755 --- a/src/adjoint/Make/options_Compressible +++ b/src/adjoint/Make/options_Compressible @@ -18,8 +18,7 @@ EXE_INC = \ -I../include \ -I$(PETSC_DIR)/include \ -I$(PETSC_DIR)/$(PETSC_ARCH)/include \ - -I$(MPI_ARCH_PATH)/include \ - -I$(MPI_ARCH_PATH)/include64 \ + $(shell mpicc -show | grep -o '\-I[^ ]*') \ $(shell python3-config --includes) @@ -35,7 +34,6 @@ LIB_LIBS = \ -lmeshTools$(DF_LIB_SUFFIX) \ -lfvOptions$(DF_LIB_SUFFIX) \ -L$(PETSC_LIB) -lpetsc \ - -L$(MPI_ARCH_PATH)/lib \ - -L$(MPI_ARCH_PATH)/lib64 \ + $(shell mpicc -show | grep -o '\-L[^ ]*') \ $(shell python3-config --ldflags) \ -fno-lto diff --git a/src/adjoint/Make/options_Incompressible b/src/adjoint/Make/options_Incompressible index 0d51583c..055bb4f0 100755 --- a/src/adjoint/Make/options_Incompressible +++ b/src/adjoint/Make/options_Incompressible @@ -22,8 +22,7 @@ EXE_INC = \ -I../include \ -I$(PETSC_DIR)/include \ -I$(PETSC_DIR)/$(PETSC_ARCH)/include \ - -I$(MPI_ARCH_PATH)/include \ - -I$(MPI_ARCH_PATH)/include64 \ + $(shell mpicc -show | grep -o '\-I[^ ]*') \ $(shell python3-config --includes) @@ -41,7 +40,6 @@ LIB_LIBS = \ -ldynamicMesh$(DF_LIB_SUFFIX) \ -lfvOptions$(DF_LIB_SUFFIX) \ -L$(PETSC_LIB) -lpetsc \ - -L$(MPI_ARCH_PATH)/lib \ - -L$(MPI_ARCH_PATH)/lib64 \ + $(shell mpicc -show | grep -o '\-L[^ ]*') \ $(shell python3-config --ldflags) \ -fno-lto diff --git a/src/adjoint/Make/options_Solid b/src/adjoint/Make/options_Solid index 8482e4d1..2371a343 100755 --- a/src/adjoint/Make/options_Solid +++ b/src/adjoint/Make/options_Solid @@ -13,8 +13,7 @@ EXE_INC = \ -I../include \ -I$(PETSC_DIR)/include \ -I$(PETSC_DIR)/$(PETSC_ARCH)/include \ - -I$(MPI_ARCH_PATH)/include \ - -I$(MPI_ARCH_PATH)/include64 \ + $(shell mpicc -show | grep -o '\-I[^ ]*') \ $(shell python3-config --includes) @@ -24,7 +23,6 @@ LIB_LIBS = \ -lmeshTools$(DF_LIB_SUFFIX) \ -lsampling$(DF_LIB_SUFFIX) \ -L$(PETSC_LIB) -lpetsc \ - -L$(MPI_ARCH_PATH)/lib \ - -L$(MPI_ARCH_PATH)/lib64 \ + $(shell mpicc -show | grep -o '\-L[^ ]*') \ $(shell python3-config --ldflags) \ -fno-lto