Skip to content

Commit

Permalink
Added regression, unit, and coverage test templates (#724)
Browse files Browse the repository at this point in the history
* Fixed a bug for the distributed flag for functions

* Added DASimpleFoam test.

* Fixed codecov test.

* Fixed a bug in codecov

* Added Python coverage.

* Added more tracking files.

* Fixed a codecov typo

* Changed the all run script for test such that it will run all the tests.

* Added checkMesh and printRes to primals.
  • Loading branch information
friedenhe authored Dec 19, 2024
1 parent 4d04843 commit 1c6bf76
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 415 deletions.
20 changes: 6 additions & 14 deletions .github/workflows/code_cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,25 @@ env:
DOCKER_MOUNT_DIR: '/home/dafoamuser/mount/$REPO_NAME'
DOCKER_TAG: 'v4'
DOCKER_ENV_FILE: '/home/dafoamuser/dafoam/loadDAFoam.sh'
DOCKER_OF_ADF_BASHRC: '/home/dafoamuser/dafoam/OpenFOAM/OpenFOAM-v1812-ADF/etc/bashrc'
DOCKER_OF_ADR_BASHRC: '/home/dafoamuser/dafoam/OpenFOAM/OpenFOAM-v1812-ADR/etc/bashrc'

jobs:
code_coverage:
runs-on: ubuntu-20.04
name: Codecov
strategy:
fail-fast: false
matrix:
testConfig: [incompressible]
include:
- testConfig: incompressible
args: 'incompressible'

steps:
- uses: actions/checkout@main
- name: Create the docker container and run the tests
run: |
docker pull dafoam/opt-packages:${{env.DOCKER_TAG}}
docker run -i -d -u dafoamuser --name regtest -v $GITHUB_WORKSPACE:${{env.DOCKER_MOUNT_DIR}} dafoam/opt-packages:${{env.DOCKER_TAG}} /bin/bash
docker exec -i regtest /bin/bash -c "rm -rf ${{env.DOCKER_WORKING_DIR}} && cp -r ${{env.DOCKER_MOUNT_DIR}} ${{env.DOCKER_WORKING_DIR}}"
docker exec regtest sed -i 's/-std=c++11/-std=c++11 -fprofile-arcs -ftest-coverage/g' ${{env.DOCKER_WORKING_DIR}}/src/adjoint/DAOption/Make/options
docker exec regtest sed -i 's/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX)/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX) -lgcov/g' ${{env.DOCKER_WORKING_DIR}}/src/adjoint/DAOption/Make/options
docker exec regtest sed -i 's/-std=c++11/-std=c++11 -fprofile-arcs -ftest-coverage/g' ${{env.DOCKER_WORKING_DIR}}/src/adjoint/DAUtility/Make/options
docker exec regtest sed -i 's/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX)/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX) -lgcov/g' ${{env.DOCKER_WORKING_DIR}}/src/adjoint/DAUtility/Make/options
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/src && . ./.add_coverage_flag.sh"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && pip install ."
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && ./Allrun"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && . ${{env.DOCKER_OF_ADR_BASHRC}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake 2> log && pip install ."
docker exec -i -e DF_CHECK_COVERAGE=1 regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && ./Allrun"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && coverage combine && coverage xml && echo dafoamuser | sudo -S cp -r coverage.xml ${{env.DOCKER_MOUNT_DIR}}/"
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}}/"
- name: Upload
uses: codecov/codecov-action@v4
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ jobs:
reg_tests:
runs-on: ubuntu-20.04
name: Tests
strategy:
fail-fast: false
matrix:
testConfig: [incompressible]
include:
- testConfig: incompressible
args: 'incompressible'
steps:
- uses: actions/checkout@v3
- name: Create the docker container and run the tests
Expand All @@ -31,5 +24,5 @@ jobs:
docker run -i -d -u dafoamuser --name regtest -v $GITHUB_WORKSPACE:${{env.DOCKER_MOUNT_DIR}} dafoam/opt-packages:${{env.DOCKER_TAG}} /bin/bash
docker exec -i regtest /bin/bash -c "rm -rf ${{env.DOCKER_WORKING_DIR}} && cp -r ${{env.DOCKER_MOUNT_DIR}} ${{env.DOCKER_WORKING_DIR}}"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && . ${{env.DOCKER_OF_ADR_BASHRC}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake && pip install ."
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && . ${{env.DOCKER_OF_ADR_BASHRC}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake 2> log && pip install ."
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && ./Allrun"
13 changes: 12 additions & 1 deletion dafoam/mphys/mphys_dafoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,20 @@ def solve_nonlinear(self, inputs, outputs):
DASolver.setThermal(q_conduct)
else:
raise AnalysisError("discipline not valid!")

# before running the primal, we need to check if the mesh
# quality is good
meshOK = DASolver.solver.checkMesh()

# solve the flow with the current design variable
DASolver()
# if the mesh is not OK, do not run the primal
if meshOK:
DASolver()
else:
DASolver.primalFail = 1

# after solving the primal, we need to print its residual info
DASolver.solver.calcPrimalResidualStatistics("print".encode())

# get the objective functions
funcs = {}
Expand Down
10 changes: 10 additions & 0 deletions src/.add_coverage_flag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

sed -i 's/-std=c++11/-std=c++11 -fprofile-arcs -ftest-coverage/g' adjoint/*/Make/options
sed -i 's/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX)/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX) -lgcov/g' adjoint/*/Make/options

sed -i 's/-std=c++11/-std=c++11 -fprofile-arcs -ftest-coverage/g' pyDASolvers/Make/options
sed -i 's/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX)/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX) -lgcov/g' pyDASolvers/Make/options

sed -i 's/-std=c++11/-std=c++11 -fprofile-arcs -ftest-coverage/g' utilities/coloring/Make/options
sed -i 's/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX)/-lfiniteVolume$(WM_CODI_AD_LIB_POSTFIX) -lgcov/g' utilities/coloring/Make/options
1 change: 1 addition & 0 deletions src/adjoint/models/Make/files
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MRFDF/MRFZoneDF.C
MRFDF/MRFZoneListDF.C
MRFDF/IOMRFZoneListDF.C
meshWaveFrozen/meshWaveFrozenPatchDistMethod.C

LIB = $(DAFOAM_ROOT_PATH)/OpenFOAM/sharedLibs/libDAMisc$(WM_CODI_AD_LIB_POSTFIX)
65 changes: 61 additions & 4 deletions tests/Allrun
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,68 @@ else
wget https://github.com/DAFoam/reg_test_files/archive/refs/heads/main.tar.gz -O reg_test_files-main.tar.gz --no-check-certificate
fi

function runUnitTests()
function runRegTests()
{
rm -rf reg_test_files-main
rm -rf reg_test_files-main DAFoam_Test_${1}.txt
tar -zxf reg_test_files-main.tar.gz
mpirun --oversubscribe -np 4 python runUnitTests_${1}.py
if [ -z "$DF_CHECK_COVERAGE" ]; then
mpirun --oversubscribe -np 4 python runRegTests_${1}.py $@ | tee DAFoam_Test_${1}.txt
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
echo "${1}: Failed!"
exit 1
fi
# need to replace the "[0m" for mphys tests
sed -i 's/\[0m//g' DAFoam_Test_${1}.txt
sed -i 's/[^[:print:]\t]//g' DAFoam_Test_${1}.txt
python testFuncs.py refs/DAFoam_Test_${1}Ref.txt DAFoam_Test_${1}.txt
if [ "$?" -ne "0" ]; then
echo "${1}: Failed!"
exit 1
else
echo "${1}: Success!"
fi
elif [ "$DF_CHECK_COVERAGE" = "1" ]; then
mpirun --oversubscribe -np 4 coverage run runRegTests_${1}.py $@ | tee DAFoam_Test_${1}.txt
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
echo "${1}: Failed!"
exit 1
fi
echo "DF_CHECK_COVERAGE key found! Do NOT check the regression test values!"
else
echo "DF_CHECK_COVERAGE key not valid! Set it to 1!"
exit 1
fi
}

runUnitTests DAUtility
function runUnitTests()
{
rm -rf reg_test_files-main DAFoam_Test_${1}.txt
tar -zxf reg_test_files-main.tar.gz
mpirun --oversubscribe -np 4 python runUnitTests_${1}.py
}

# Find all files matching the regression test script pattern
for file in runRegTests_*.py; do
# Check if the file exists to avoid wildcard issues
if [[ -f "$file" ]]; then
# Extract the part between "runRegTests_" and ".py"
extracted_name="${file#runRegTests_}"
extracted_name="${extracted_name%.py}"

# run the regression tests
runRegTests "$extracted_name"
fi
done

# Find all files matching the unit test script pattern
for file in runUnitTests_*.py; do
# Check if the file exists to avoid wildcard issues
if [[ -f "$file" ]]; then
# Extract the part between "runUnitTests_" and ".py"
extracted_name="${file#runUnitTests_}"
extracted_name="${extracted_name%.py}"

# run the regression tests
runUnitTests "$extracted_name"
fi
done
129 changes: 11 additions & 118 deletions tests/refs/DAFoam_Test_DASimpleFoamRef.txt
Original file line number Diff line number Diff line change
@@ -1,121 +1,14 @@
Dictionary Key: CD
@value 0.01438365115696084 1e-08 1e-10
@value 0.01707374542774201 1e-10 1e-12
Dictionary Key: CL
@value 0.5278433423293855 1e-08 1e-10
Dictionary Key: CMZ
@value -0.006992993721841563 1e-08 1e-10
Dictionary Key: THRUST
@value 0.3964124860392292 1e-08 1e-10
Dictionary Key: VOL
@value 32.90556883062804 1e-08 1e-10
Dictionary Key: fail
@value 0 1e-08 1e-10
Dictionary Key: forces
@value 0.449611137104235 1e-08 1e-10
Dictionary Key: CD
Dictionary Key: actuator
@value 0.002692654743062405 1e-04 1e-06
@value -0.0003473341347664285 1e-04 1e-06
@value 5.620234383570104e-05 1e-04 1e-06
@value 0 0.0001 1e-06
@value -0.00277322483067405 0.0001 1e-06
@value -1.890611842632963e-07 0.0001 1e-06
@value -0.005953704788967595 1e-04 1e-06
@value 9.593947158077255e-05 1e-04 1e-06
@value 1.956078536327928e-05 1e-04 1e-06
@value 1.534919864800157e-08 1e-04 1e-06
@value -0.001740961838225806 1e-04 1e-06
@value -0.001285589164731539 1e-04 1e-06
@value 0 0.0001 1e-06
Dictionary Key: alpha
@value 0.003478846233720689 1e-04 1e-06
Dictionary Key: shapey
@value -0.01145880278936309 1e-04 1e-06
@value 0.04946171930909396 1e-04 1e-06
@value 0.09099507726500972 1e-04 1e-06
@value 0.2998610077528413 1e-10 1e-12
Dictionary Key: CL
Dictionary Key: actuator
@value -0.01775995579432492 1e-04 1e-06
@value 0.1160533063360002 1e-04 1e-06
@value 0.00188311916616678 1e-04 1e-06
@value 0 0.0001 1e-06
@value 0.02316872166184394 0.0001 1e-06
@value -6.046074582706222e-07 0.0001 1e-06
@value -0.1581267547150706 1e-04 1e-06
@value -0.03433514066432578 1e-04 1e-06
@value 0.0002027111536501591 1e-04 1e-06
@value -8.172073868387086e-07 1e-04 1e-06
@value -0.02806802638939627 1e-04 1e-06
@value -0.005537900303758031 1e-04 1e-06
@value 0 0.0001 1e-06
Dictionary Key: alpha
@value 0.09973980264096334 1e-04 1e-06
Dictionary Key: shapey
@value 1.559463012159858 1e-04 1e-06
@value 1.231602807591228 1e-04 1e-06
@value 1.803088011202189 1e-04 1e-06
Dictionary Key: CMZ
Dictionary Key: actuator
@value -0.001714635571617953 1e-04 1e-06
@value -0.0005710092451789862 1e-04 1e-06
@value 0.0001053970922245933 1e-04 1e-06
@value 0 0.0001 1e-06
@value -0.002738231252915331 0.0001 1e-06
@value 3.090059231618091e-07 0.0001 1e-06
@value -0.005972163653488304 1e-04 1e-06
@value 0.0002894111646713729 1e-04 1e-06
@value -1.652421307341438e-05 1e-04 1e-06
@value -1.640852649479966e-07 1e-04 1e-06
@value 0.0004079649174387717 1e-04 1e-06
@value 0.001743591514138321 1e-04 1e-06
@value 0 0.0001 1e-06
Dictionary Key: alpha
@value -0.00252751197470288 1e-04 1e-06
Dictionary Key: shapey
@value 0.04277114768660325 1e-04 1e-06
@value 0.3266475035683201 1e-04 1e-06
@value 0.7983250993983276 1e-04 1e-06
Dictionary Key: THRUST
Dictionary Key: actuator
@value 0.3141086102615986 1e-04 1e-06
@value -0.003605129121397876 1e-04 1e-06
@value 0.007310727357323632 1e-04 1e-06
@value 0 0.0001 1e-06
@value 0.356700099055085 0.0001 1e-06
@value -5.536203717682042e-07 0.0001 1e-06
@value -0.819670401731476 1e-04 1e-06
@value 0.8428678211159779 1e-04 1e-06
@value 0.003964124860367235 1e-04 1e-06
@value 0 1e-04 1e-06
@value -0.2740304202085708 1e-04 1e-06
@value -0.3409006479776489 1e-04 1e-06
@value 0 0.0001 1e-06
Dictionary Key: alpha
@value 0 1e-04 1e-06
Dictionary Key: shapey
@value -0.009188209263988334 1e-04 1e-06
@value 0.0006339731860594 1e-04 1e-06
@value 0.0007674441613869654 1e-04 1e-06
Dictionary Key: VOL
Dictionary Key: actuator
@value -1.789350017581762 1e-04 1e-06
@value 11.26830256393616 1e-04 1e-06
@value 0.2116803123864702 1e-04 1e-06
@value 0 0.0001 1e-06
@value -2.160271468861342 0.0001 1e-06
@value -0.000198398358891418 0.0001 1e-06
@value -18.33919528265704 1e-04 1e-06
@value -2.835167655956745 1e-04 1e-06
@value 0.03030092859457912 1e-04 1e-06
@value -7.666586571084363e-05 1e-04 1e-06
@value -3.581283376190157 1e-04 1e-06
@value -1.311124366909558 1e-04 1e-06
@value 0 0.0001 1e-06
Dictionary Key: alpha
@value 10.47559304035274 1e-04 1e-06
Dictionary Key: shapey
@value 213.3053877790166 1e-04 1e-06
@value 145.9822612662322 1e-04 1e-06
@value 168.87748437004 1e-04 1e-06
Dictionary Key: fail
@value 0 1e-04 1e-06
Dictionary Key: shape
@value 1.243384514756609 1e-08 1e-12
Dictionary Key: twist
@value 0.0975429482398948 1e-08 1e-12
Dictionary Key: LoD
Dictionary Key: shape
@value 62.66435123225894 1e-08 1e-12
Dictionary Key: twist
@value 4.428296159540333 1e-08 1e-12
Loading

0 comments on commit 1c6bf76

Please sign in to comment.