Skip to content

Commit

Permalink
V2.0.4 (#58)
Browse files Browse the repository at this point in the history
* Added wallHeatFlux obj.

* Updated version.

* Added DASimpleTFoam solver.

* Added totalPressure as objective.

* Added tests for new solver and objectives.

* Updated travis.

* Added MRF for DASimpleFoam.

* Removed MRF from DASimpleFoam.

* Added DATurbFoam and its tests.

* Updated travis.

* Added DASolidDisplacementFoam. Re-organized lib structure with a new condition Solid that does not depends on turbulence models.

* Added tests for DASolidDisplacementFoam.

* Fixed the pressureInletVelocity BC and updated tests.

* Fixed an issue for dFdW for vonMisesStressKS.

* Fixed an issue for DATurbFoam to improve sens accuracy.

* Updated tests for DATurbFoam.
  • Loading branch information
friedenhe authored Sep 5, 2020
1 parent f64a2ba commit 0ef51fb
Show file tree
Hide file tree
Showing 104 changed files with 6,158 additions and 94 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
- docker exec -it regtest /bin/bash -c "pip install cpp-coveralls --user && pip install coverage --user"
# compile and run the tests
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DASimpleFoam.py noCheckVal"
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DASimpleTFoam.py noCheckVal"
# run coveralls, the -lp option is for inline functions in C++
- docker exec -it -e TRAVIS="$TRAVIS" -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" -e TRAVIS_BRANCH="$TRAVIS_BRANCH" regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/src/adjoint && cp -r ../include ./Make/linux*/DASolver/ && cp -r ../include ./Make/linux*/ && COVERALLS_PARALLEL=true coveralls --gcov-options '\-lp'"

Expand All @@ -63,8 +64,23 @@ jobs:
- docker exec -it regtest /bin/bash -c "pip install cpp-coveralls --user && pip install coverage --user"
# compile and run the tests
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DARhoSimpleFoam.py noCheckVal"
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DARhoSimpleFoamUBend.py noCheckVal"
- docker exec -it regtest /bin/bash -c "cd $DOCKER_WORKING_DIR/tests && rm -rf input && tar -xzf input.tar.gz"
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DARhoSimpleCFoam.py noCheckVal"
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DATurboFoamSubsonic.py noCheckVal"
- docker exec -it regtest /bin/bash -c "cd $DOCKER_WORKING_DIR/tests && rm -rf input && tar -xzf input.tar.gz"
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DATurboFoamTransonic.py noCheckVal"
# run coveralls, the -lp option is for inline functions in C++
- docker exec -it -e TRAVIS="$TRAVIS" -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" -e TRAVIS_BRANCH="$TRAVIS_BRANCH" regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/src/adjoint && cp -r ../include ./Make/linux*/DASolver/ && cp -r ../include ./Make/linux*/ && COVERALLS_PARALLEL=true coveralls --gcov-options '\-lp'"

- stage: cppCodeCoverageSolid
script:
# compile solid solvers
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR && ./Allmake solid && rm -rf $DOCKER_PIP_INSTALL_PATH/dafoam* && pip install ."
# install cpp-coveralls and coverage
- docker exec -it regtest /bin/bash -c "pip install cpp-coveralls --user && pip install coverage --user"
# compile and run the tests
- docker exec -it regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/tests && mpirun -np 4 python runTests_DASolidDisplacementFoam.py noCheckVal"
# run coveralls, the -lp option is for inline functions in C++
- docker exec -it -e TRAVIS="$TRAVIS" -e TRAVIS_JOB_ID="$TRAVIS_JOB_ID" -e TRAVIS_BRANCH="$TRAVIS_BRANCH" regtest /bin/bash -c ". /home/dafoamuser/setupDAFoam.sh && cd $DOCKER_WORKING_DIR/src/adjoint && cp -r ../include ./Make/linux*/DASolver/ && cp -r ../include ./Make/linux*/ && COVERALLS_PARALLEL=true coveralls --gcov-options '\-lp'"

Expand Down
4 changes: 3 additions & 1 deletion Allmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ fi
cd src/adjoint && ./Allmake $argm && cd -
cd src/pyDASolvers && ./Allmake $argm && cd -
cd src/utilities && ./Allmake $argm && cd -
cd src/models && ./Allmake && cd -
if [ "$argm" != "solid" ]; then
cd src/models && ./Allmake && cd -
fi
2 changes: 1 addition & 1 deletion dafoam/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.0.3'
__version__ = '2.0.4'

from mpi4py import MPI

Expand Down
121 changes: 71 additions & 50 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class DAOPTION(object):
Define a set of options to use in PYDAFOAM and set their initial values.
This class will be used by PYDAFOAM._getDefOptions()
NOTE: Give an initial value for a new option, this help PYDAFOAM determine the
type of this option. Also, use ## to add comments before the new option such that
these comments will be picked up by Doxygen. If possible, give examples.
NOTE: Give an initial value for a new option, this help PYDAFOAM determine the type of this
option. If it is a list, give at least one default value. If it is a dict, you can leave it
blank, e.g., {}. Also, use ## to add comments before the new option such that these comments
will be picked up by Doxygen. If possible, give examples.
NOTE: We group these options into three categories.
- The basic options are those options that will be used for EVERY solvers and EVERY cases.
Expand Down Expand Up @@ -280,6 +281,7 @@ class DAOPTION(object):
"epsilonRes": 2,
"omegaRes": 2,
"p_rghRes": 2,
"DRes": 2,
}

## The min bound for Jacobians, any value that is smaller than the bound will be set to 0
Expand All @@ -289,13 +291,13 @@ class DAOPTION(object):
"dRdWPC": 1.0e-30,
}

## The maximal iterations of tractionDisplacement boundary conditions
maxTractionBCIters = 100

## decomposeParDict option. This file will be automatically written such that users
## can run optimization with any number of CPU cores without the need to manually
## can run optimization with any number of CPU cores without the need to manually
## change decomposeParDict
decomposeParDict = {
"method": "scotch",
"simpleCoeffs": {"n": [2, 2, 1], "delta": 0.001}
}
decomposeParDict = {"method": "scotch", "simpleCoeffs": {"n": [2, 2, 1], "delta": 0.001}, "preservePatches": ["None"]}

## The ordering of state variable. Options are: state or cell. Most of the case, the state
## odering is the best choice.
Expand Down Expand Up @@ -1022,6 +1024,11 @@ def _initSolver(self):

from .pyDASolverCompressible import pyDASolvers

self.solver = pyDASolvers(solverArg.encode(), self.options)
elif self.getOption("flowCondition") == "Solid":

from .pyDASolverSolid import pyDASolvers

self.solver = pyDASolvers(solverArg.encode(), self.options)
else:
raise Error("pyDAFoam: flowCondition %s: not valid!" % self.getOption("flowCondition"))
Expand Down Expand Up @@ -1056,6 +1063,12 @@ def runColoring(self):

solverArg = "ColoringCompressible -python " + self.parallelFlag
solver = pyColoringCompressible(solverArg.encode(), self.options)
elif self.getOption("flowCondition") == "Solid":

from .pyColoringSolid import pyColoringSolid

solverArg = "ColoringSolid -python " + self.parallelFlag
solver = pyColoringSolid(solverArg.encode(), self.options)
else:
raise Error("pyDAFoam: flowCondition %s: not valid!" % self.getOption("flowCondition"))
solver.run()
Expand Down Expand Up @@ -1806,65 +1819,73 @@ def _getImmutableOptions(self):

# return ("meshSurfaceFamily", "designSurfaceFamily")
return ()

def _writeDecomposeParDict(self):
"""
Write system/decomposeParDict
"""
if self.comm.rank == 0:
# Open the options file for writing
workingDirectory = os.getcwd()
sysDir = 'system'
workingDirectory = os.getcwd()
sysDir = "system"
varDir = os.path.join(workingDirectory, sysDir)
fileName = 'decomposeParDict'
fileName = "decomposeParDict"
fileLoc = os.path.join(varDir, fileName)
f = open(fileLoc, 'w')
# write header
f = open(fileLoc, "w")
# write header
self._writeOpenFoamHeader(f, "dictionary", sysDir, fileName)
# write content
decomDict=self.getOption('decomposeParDict')
n = decomDict['simpleCoeffs']['n']
f.write('numberOfSubdomains %d;\n'%self.nProcs)
f.write('\n')
f.write('method %s;\n'%decomDict['method'])
f.write('\n')
f.write('simpleCoeffs \n')
f.write('{ \n')
f.write(' n (%d %d %d);\n'%(n[0], n[1], n[2]))
f.write(' delta %g;\n'%decomDict['simpleCoeffs']['delta'])
f.write('} \n')
f.write('\n')
f.write('distributed false;\n')
f.write('\n')
f.write('roots();\n')
f.write('\n')
f.write('// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n')
decomDict = self.getOption("decomposeParDict")
n = decomDict["simpleCoeffs"]["n"]
f.write("numberOfSubdomains %d;\n" % self.nProcs)
f.write("\n")
f.write("method %s;\n" % decomDict["method"])
f.write("\n")
f.write("simpleCoeffs \n")
f.write("{ \n")
f.write(" n (%d %d %d);\n" % (n[0], n[1], n[2]))
f.write(" delta %g;\n" % decomDict["simpleCoeffs"]["delta"])
f.write("} \n")
f.write("\n")
f.write("distributed false;\n")
f.write("\n")
f.write("roots();\n")
if len(decomDict["preservePatches"]) == 1 and decomDict["preservePatches"][0] == "None":
pass
else:
f.write("\n")
f.write("preservePatches (")
for pPatch in decomDict["preservePatches"]:
f.write("%s " % pPatch)
f.write(");\n")
f.write("\n")
f.write("// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n")

f.close()
self.comm.Barrier()

def _writeOpenFoamHeader(self, f, className, location, objectName):
"""
Write OpenFOAM header file
"""
f.write('/*--------------------------------*- C++ -*---------------------------------*\ \n')
f.write('| ======== | | \n')
f.write('| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | \n')
f.write('| \\ / O peration | Version: v1812 | \n')
f.write('| \\ / A nd | Web: www.OpenFOAM.com | \n')
f.write('| \\/ M anipulation | | \n')
f.write('\*--------------------------------------------------------------------------*/ \n')
f.write('FoamFile\n')
f.write('{\n')
f.write(' version 2.0;\n')
f.write(' format ascii;\n')
f.write(' class %s;\n'%className)
f.write(' location "%s";\n'%location)
f.write(' object %s;\n'%objectName)
f.write('}\n')
f.write('// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n')
f.write('\n')

f.write("/*--------------------------------*- C++ -*---------------------------------*\ \n")
f.write("| ======== | | \n")
f.write("| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | \n")
f.write("| \\ / O peration | Version: v1812 | \n")
f.write("| \\ / A nd | Web: www.OpenFOAM.com | \n")
f.write("| \\/ M anipulation | | \n")
f.write("\*--------------------------------------------------------------------------*/ \n")
f.write("FoamFile\n")
f.write("{\n")
f.write(" version 2.0;\n")
f.write(" format ascii;\n")
f.write(" class %s;\n" % className)
f.write(' location "%s";\n' % location)
f.write(" object %s;\n" % objectName)
f.write("}\n")
f.write("// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n")
f.write("\n")


class Error(Exception):
Expand Down
16 changes: 15 additions & 1 deletion src/adjoint/Allmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ case $1 in
cp Make/files_Compressible Make/files
cp Make/options_Compressible Make/options
wmake -j 4 || exit 1
./Allclean
cp Make/files_Solid Make/files
cp Make/options_Solid Make/options
wmake -j 4 || exit 1
;;
"debug")
cp Make/files_Incompressible Make/files
Expand All @@ -25,6 +29,11 @@ case $1 in
cp Make/options_Compressible Make/options
sed -i 's/-std=c++11/-std=c++11 -DFULLDEBUG -g -O0/g' Make/options
wmake -j 4 || exit 1
./Allclean
cp Make/files_Solid Make/files
cp Make/options_Solid Make/options
sed -i 's/-std=c++11/-std=c++11 -DFULLDEBUG -g -O0/g' Make/options
wmake -j 4 || exit 1
;;
"incompressible")
cp Make/files_Incompressible Make/files
Expand All @@ -36,8 +45,13 @@ case $1 in
cp Make/options_Compressible Make/options
wmake -j 4 || exit 1
;;
"solid")
cp Make/files_Solid Make/files
cp Make/options_Solid Make/options
wmake -j 4 || exit 1
;;
*)
echo "Argument not valid! Options are: opt, debug, incompressible, and compressible"
echo "Argument not valid! Options are: opt, debug, incompressible, compressible, or solid"
echo "Example: ./Allmake opt"
exit 1
;;
Expand Down
68 changes: 68 additions & 0 deletions src/adjoint/DAField/DAField.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ DAField::DAField(
word solverName = daOption.getOption<word>("solverName");
autoPtr<DAStateInfo> daStateInfo(DAStateInfo::New(solverName, mesh, daOption, daModel));
stateInfo_ = daStateInfo->getStateInfo();

// check if we have special boundary conditions that need special treatment
this->checkSpecialBCs();
}

void DAField::ofField2StateVec(Vec stateVec) const
Expand Down Expand Up @@ -475,6 +478,71 @@ void DAField::resVec2OFResField(const Vec resVec) const
VecRestoreArrayRead(resVec, &stateResVecArray);
}

void DAField::checkSpecialBCs()
{
/*
Description:
Check if we need to do special treatment for boundary conditions
If any special BC is detected, append their names to specialBCs list
*/

// *******************************************************************
// pressureInletVelocity
// *******************************************************************
// Note we need to read the U field, instead of getting it from db
// this is because coloringSolver does not read U
// Also we need to set read_if_present for solid solvers in which
// there is no U field
volVectorField U(
IOobject(
"U",
mesh_.time().timeName(),
mesh_,
IOobject::READ_IF_PRESENT,
IOobject::NO_WRITE,
false),
mesh_,
dimensionedVector("U", dimensionSet(0, 1, -1, 0, 0, 0, 0), vector::zero),
zeroGradientFvPatchField<vector>::typeName);
forAll(U.boundaryField(), patchI)
{
if (U.boundaryFieldRef()[patchI].type() == "pressureInletVelocity")
{
specialBCs.append("pressureInletVelocity");
break;
}
}

// *******************************************************************
// append more special BCs
// *******************************************************************
}

void DAField::specialBCTreatment()
{
/*
Description:
Apply special treatment for boundary conditions
*/

// *******************************************************************
// pressureInletVelocity
// *******************************************************************
// for pressureInletVelocity, the inlet U depends on
// rho and phi, so we need to call U.correctBoundaryConditions again
if (DAUtility::isInList<word>("pressureInletVelocity", specialBCs))
{

volVectorField& U(const_cast<volVectorField&>(
mesh_.thisDb().lookupObject<volVectorField>("U")));
U.correctBoundaryConditions();
}

// *******************************************************************
// append more special BCs
// *******************************************************************
}

void DAField::setPrimalBoundaryConditions()
{
/*
Expand Down
9 changes: 9 additions & 0 deletions src/adjoint/DAField/DAField.H
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public:
/// set the boundary conditions based on parameters defined in DAOption
void setPrimalBoundaryConditions();

/// apply special treatment for boundary conditions
void specialBCTreatment();

/// check if we need to do special treatment for boundary conditions
void checkSpecialBCs();

/// a list that contains the names of detected special boundary conditions
wordList specialBCs;

};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
Loading

0 comments on commit 0ef51fb

Please sign in to comment.