Skip to content

Commit 4966835

Browse files
authored
Added format tests for C++ and Python codes (#773)
* Added auto cpp format check. * Added pyproject for black. * Removed the unused tests. * Changed all py files with black. * Format tests. * Format test successful.
1 parent cf91033 commit 4966835

File tree

173 files changed

+339
-10280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+339
-10280
lines changed

Diff for: .github/workflows/cpp_format.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Cpp_format
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
Cpp_format:
7+
runs-on: ubuntu-20.04
8+
9+
steps:
10+
- name: Check out the code
11+
uses: actions/checkout@v3
12+
13+
- name: Install clang-format
14+
run: sudo apt-get install -y clang-format
15+
16+
- name: Run clang-format
17+
run: |
18+
find . -regex '.*\.\(C\|H\)' -exec clang-format -style=file -i {} \;
19+
git diff --exit-code
20+
continue-on-error: false
21+
22+
- name: Fail if formatting changes are needed
23+
if: failure()
24+
run: |
25+
echo "C++ code is not properly formatted. Please run clang-format and commit the changes."
26+
exit 1

Diff for: .github/workflows/python_format.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Python_format
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
Python_format:
7+
runs-on: ubuntu-20.04
8+
9+
steps:
10+
- name: Check out the code
11+
uses: actions/checkout@v3
12+
13+
- name: Set up Python
14+
uses: actions/setup-python@v5
15+
with:
16+
python-version: '3.8'
17+
18+
- name: Install black
19+
run: pip install black
20+
21+
- name: Run black for format check
22+
run: black --check --diff .
23+
24+
- name: Fail if formatting issues are found
25+
if: failure()
26+
run: |
27+
echo "Python code is not properly formatted. Please run 'black .' and commit the changes."
28+
exit 1

Diff for: dafoam/libs/ADF/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# empty_file
1+
# empty_file

Diff for: dafoam/libs/ADR/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# empty_file
1+
# empty_file

Diff for: dafoam/libs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# empty_file
1+
# empty_file

Diff for: dafoam/scripts/dafoam_matdiff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def evalMatDiff(mat1, mat2, mode, diffTol=1e-30):
5353
valError = valDiff
5454
else:
5555
print("mode not supported! Options are: abs or rel")
56-
l2norm = l2norm + valDiff ** 2
56+
l2norm = l2norm + valDiff**2
5757
if abs(valError) > diffTol:
5858
if abs(valError) > maxDiff:
5959
foundDiff = 1

Diff for: dafoam/scripts/dafoam_matgetvalues.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from petsc4py import PETSc
1111

1212

13-
def printMatValues(mat, rowI, colI, transposed, diffTol = 1e-30):
13+
def printMatValues(mat, rowI, colI, transposed, diffTol=1e-30):
1414

1515
# read the Jac mat
1616
jacMat = PETSc.Mat().create(PETSc.COMM_WORLD)

Diff for: dafoam/scripts/dafoam_plot3dtransform.py

+22-34
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
scaleX = float(sys.argv[4])
2828
scaleY = float(sys.argv[5])
2929
scaleZ = float(sys.argv[6])
30-
30+
3131
print(
3232
"Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g ...."
3333
% (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
3434
)
35-
35+
3636
ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
37-
37+
3838
for ivol in range(ffd.nVol):
3939
vals = ffd.vols[ivol].coef
4040
vals[:, :, :, 0] = vals[:, :, :, 0] * scaleX
4141
vals[:, :, :, 1] = vals[:, :, :, 1] * scaleY
4242
vals[:, :, :, 2] = vals[:, :, :, 2] * scaleZ
4343
ffd.writePlot3dCoef(outputFileName)
44-
44+
4545
print(
4646
"Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g Done!"
4747
% (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
@@ -50,36 +50,27 @@
5050
dX = float(sys.argv[4])
5151
dY = float(sys.argv[5])
5252
dZ = float(sys.argv[6])
53-
54-
print(
55-
"Translating %s to %s with dX: %g dY: %g dZ: %g ...."
56-
% (inputFileName, outputFileName, dX, dY, dZ)
57-
)
58-
53+
54+
print("Translating %s to %s with dX: %g dY: %g dZ: %g ...." % (inputFileName, outputFileName, dX, dY, dZ))
55+
5956
ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
60-
57+
6158
for ivol in range(ffd.nVol):
6259
vals = ffd.vols[ivol].coef
6360
vals[:, :, :, 0] = vals[:, :, :, 0] + dX
6461
vals[:, :, :, 1] = vals[:, :, :, 1] + dY
6562
vals[:, :, :, 2] = vals[:, :, :, 2] + dZ
6663
ffd.writePlot3dCoef(outputFileName)
67-
68-
print(
69-
"Translating %s to %s with dX: %g dT: %g dZ: %g Done!"
70-
% (inputFileName, outputFileName, dX, dY, dZ)
71-
)
64+
65+
print("Translating %s to %s with dX: %g dT: %g dZ: %g Done!" % (inputFileName, outputFileName, dX, dY, dZ))
7266
elif mode == "rotate":
7367
axis = str(sys.argv[4])
7468
deg = float(sys.argv[5])
7569

76-
print(
77-
"Rotating %s to %s wrt to the %s axis by %g degree...."
78-
% (inputFileName, outputFileName, axis, deg)
79-
)
70+
print("Rotating %s to %s wrt to the %s axis by %g degree...." % (inputFileName, outputFileName, axis, deg))
8071

8172
ffd = pyBlock("plot3d", fileName=inputFileName, FFD=True)
82-
73+
8374
theta = deg * np.pi / 180.0
8475

8576
for ivol in range(ffd.nVol):
@@ -90,26 +81,23 @@
9081
if axis == "x":
9182
yRef = vals[i, j, k, 1]
9283
zRef = vals[i, j, k, 2]
93-
vals[i, j, k, 1] = np.cos(theta) * yRef - np.sin(theta) * zRef
94-
vals[i, j, k, 2] = np.sin(theta) * yRef + np.cos(theta) * zRef
84+
vals[i, j, k, 1] = np.cos(theta) * yRef - np.sin(theta) * zRef
85+
vals[i, j, k, 2] = np.sin(theta) * yRef + np.cos(theta) * zRef
9586
elif axis == "y":
9687
xRef = vals[i, j, k, 0]
9788
zRef = vals[i, j, k, 2]
98-
vals[i, j, k, 0] = np.cos(theta) * xRef + np.sin(theta) * zRef
99-
vals[i, j, k, 2] = -np.sin(theta) * xRef + np.cos(theta) * zRef
89+
vals[i, j, k, 0] = np.cos(theta) * xRef + np.sin(theta) * zRef
90+
vals[i, j, k, 2] = -np.sin(theta) * xRef + np.cos(theta) * zRef
10091
elif axis == "z":
10192
xRef = vals[i, j, k, 0]
10293
yRef = vals[i, j, k, 1]
103-
vals[i, j, k, 0] = np.cos(theta) * xRef - np.sin(theta) * yRef
104-
vals[i, j, k, 1] = np.sin(theta) * xRef + np.cos(theta) * yRef
94+
vals[i, j, k, 0] = np.cos(theta) * xRef - np.sin(theta) * yRef
95+
vals[i, j, k, 1] = np.sin(theta) * xRef + np.cos(theta) * yRef
10596
else:
106-
print("Axis %s not supported! Options are: x, y, or z"%s)
107-
97+
print("Axis %s not supported! Options are: x, y, or z" % s)
98+
10899
ffd.writePlot3dCoef(outputFileName)
109100

110-
print(
111-
"Rotating %s to %s wrt to the %s axis by %g degree Done!"
112-
% (inputFileName, outputFileName, axis, deg)
113-
)
101+
print("Rotating %s to %s wrt to the %s axis by %g degree Done!" % (inputFileName, outputFileName, axis, deg))
114102
else:
115-
print("Mode %s not supported! Options are: scale, translate, or rotate"%s)
103+
print("Mode %s not supported! Options are: scale, translate, or rotate" % s)

Diff for: dafoam/scripts/dafoam_stltransform.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
scaleX = float(sys.argv[4])
2828
scaleY = float(sys.argv[5])
2929
scaleZ = float(sys.argv[6])
30-
30+
3131
print(
3232
"Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g ...."
3333
% (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
3434
)
35-
35+
3636
myMesh = mesh.Mesh.from_file(inputFileName)
3737

3838
myMesh.x *= scaleX
3939
myMesh.y *= scaleY
4040
myMesh.z *= scaleZ
4141

4242
myMesh.save(outputFileName)
43-
43+
4444
print(
4545
"Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g Done!"
4646
% (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
@@ -49,40 +49,31 @@
4949
dX = float(sys.argv[4])
5050
dY = float(sys.argv[5])
5151
dZ = float(sys.argv[6])
52-
53-
print(
54-
"Translating %s to %s with dX: %g dY: %g dZ: %g ...."
55-
% (inputFileName, outputFileName, dX, dY, dZ)
56-
)
57-
52+
53+
print("Translating %s to %s with dX: %g dY: %g dZ: %g ...." % (inputFileName, outputFileName, dX, dY, dZ))
54+
5855
myMesh = mesh.Mesh.from_file(inputFileName)
5956

6057
myMesh.x += dX
6158
myMesh.y += dY
6259
myMesh.z += dZ
6360

6461
myMesh.save(outputFileName)
65-
66-
print(
67-
"Translating %s to %s with dX: %g dT: %g dZ: %g Done!"
68-
% (inputFileName, outputFileName, dX, dY, dZ)
69-
)
62+
63+
print("Translating %s to %s with dX: %g dT: %g dZ: %g Done!" % (inputFileName, outputFileName, dX, dY, dZ))
7064
elif mode == "rotate":
7165
axis = str(sys.argv[4])
7266
deg = float(sys.argv[5])
7367

74-
print(
75-
"Rotating %s to %s wrt to the %s axis by %g degree...."
76-
% (inputFileName, outputFileName, axis, deg)
77-
)
68+
print("Rotating %s to %s wrt to the %s axis by %g degree...." % (inputFileName, outputFileName, axis, deg))
7869

7970
# ************** NOTE ******************
8071
# we have to add a minus sign here because numpy-stl is known
8172
# to have a problem for the direction of the rotation. It somehow rotates
8273
# clockwise for a positive angle, which is not consistent with the conventional
8374
# right-hand-side rule of rotation.
8475
# Check their documentation https://numpy-stl.readthedocs.io/en/latest/stl.html
85-
theta = - deg * np.pi / 180.0
76+
theta = -deg * np.pi / 180.0
8677

8778
myMesh = mesh.Mesh.from_file(inputFileName)
8879

@@ -93,13 +84,10 @@
9384
elif axis == "z":
9485
myMesh.rotate([0.0, 0.0, 1.0], theta)
9586
else:
96-
print("Axis %s not supported! Options are: x, y, or z"%s)
87+
print("Axis %s not supported! Options are: x, y, or z" % s)
9788

9889
myMesh.save(outputFileName)
9990

100-
print(
101-
"Rotating %s to %s wrt to the %s axis by %g degree Done!"
102-
% (inputFileName, outputFileName, axis, deg)
103-
)
91+
print("Rotating %s to %s wrt to the %s axis by %g degree Done!" % (inputFileName, outputFileName, axis, deg))
10492
else:
105-
print("Mode %s not supported! Options are: scale, translate, or rotate"%s)
93+
print("Mode %s not supported! Options are: scale, translate, or rotate" % s)

Diff for: dafoam/scripts/dafoam_vecdiff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def evalVecDiff(vecName1, vecName2, mode, diffTol=1e-30):
4949
valError = valDiff
5050
else:
5151
print("mode not supported! Options are: abs or rel")
52-
l2norm = l2norm + valDiff ** 2
52+
l2norm = l2norm + valDiff**2
5353
if abs(valError) > diffTol:
5454
if abs(valError) > maxDiff:
5555
maxDiff = valError

Diff for: dafoam/scripts/dafoam_vecgetvalues.py

-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@ def printVecValues(vecName, rowI, diffTol=1e-30):
3434
print("Example python dafoam_vecgetvalues.py dFdW.bin 100")
3535
print("NOTE: if rowI=-1, print all elements\n")
3636
printVecValues(sys.argv[1], sys.argv[2])
37-

Diff for: pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 120

Diff for: setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import re
33

44
__version__ = re.findall(
5-
r"""__version__ = ["']+([0-9\.]*)["']+""", open("dafoam/pyDAFoam.py").read(),
5+
r"""__version__ = ["']+([0-9\.]*)["']+""",
6+
open("dafoam/pyDAFoam.py").read(),
67
)[0]
78

89
setup(

Diff for: src/adjoint/DAColoring/DAColoring.H

100755100644
+5-6
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ private:
4040
void operator=(const DAColoring&);
4141

4242
protected:
43+
/// fvMesh object
44+
const fvMesh& mesh_;
4345

44-
/// fvMesh object
45-
const fvMesh& mesh_;
46-
47-
/// DAOption object
48-
const DAOption& daOption_;
46+
/// DAOption object
47+
const DAOption& daOption_;
4948

5049
/// DAIndex object
51-
const DAIndex& daIndex_;
50+
const DAIndex& daIndex_;
5251

5352
public:
5453
/// Constructors

Diff for: src/adjoint/DAFunction/DAFunctionForce.C

100755100644
+1-2
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ DAFunctionForce::DAFunctionForce(
6666
<< "Options: fixedDirection, parallelToFlow, normalToFlow."
6767
<< abort(FatalError);
6868
}
69-
69+
7070
if (fabs(mag(forceDir_) - 1.0) > 1.0e-8)
7171
{
7272
FatalErrorIn(" ") << "the magnitude of the direction parameter in "
7373
<< functionName << " is not 1.0!"
7474
<< abort(FatalError);
7575
}
76-
7776
}
7877

7978
/// calculate the value of objective function

Diff for: src/adjoint/DAFunction/DAFunctionLocation.C

100755100644
+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DAFunctionLocation::DAFunctionLocation(
5959

6060
// NOTE: we need to call a self-defined findCell func to make it work correctly in ADR
6161
snappedCenterCellI_ = DAUtility::myFindCell(mesh_, centerPoint);
62-
62+
6363
label foundCellI = 0;
6464
if (snappedCenterCellI_ >= 0)
6565
{
@@ -237,7 +237,7 @@ scalar DAFunctionLocation::calcFunction()
237237
scalar radius = mag(faceCRadial);
238238
scalar iRadius = 1.0 / (radius + 1e-12);
239239

240-
objValTmp += exp(coeffKS_ * iRadius);
240+
objValTmp += exp(coeffKS_ * iRadius);
241241

242242
if (objValTmp > 1e200)
243243
{
@@ -284,8 +284,8 @@ scalar DAFunctionLocation::calcFunction()
284284
else
285285
{
286286
FatalErrorIn("DAFunctionLocation") << "mode: " << mode_ << " not supported!"
287-
<< "Options are: maxRadius"
288-
<< abort(FatalError);
287+
<< "Options are: maxRadius"
288+
<< abort(FatalError);
289289
}
290290

291291
// check if we need to calculate refDiff.

Diff for: src/adjoint/DAFunction/DAFunctionMeshQualityKS.C

100755100644
-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ DAFunctionMeshQualityKS::DAFunctionMeshQualityKS(
6464
{
6565
Info << "includeFaceList " << includeFaceList_ << endl;
6666
}
67-
6867
}
6968

7069
/// calculate the value of objective function

Diff for: src/adjoint/DAFunction/DAFunctionMeshQualityKS.H

100755100644
-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class DAFunctionMeshQualityKS
2929
{
3030

3131
protected:
32-
3332
/// coefficient for the KS function
3433
scalar coeffKS_;
3534

@@ -42,7 +41,6 @@ protected:
4241
/// a list of included faces for the mesh quality calculation
4342
labelList includeFaceList_;
4443

45-
4644
public:
4745
TypeName("meshQualityKS");
4846
// Constructors

0 commit comments

Comments
 (0)