Skip to content

Commit dbcb957

Browse files
committed
Ruff
1 parent 90f24ab commit dbcb957

8 files changed

+1634
-561
lines changed

.github/workflows/main.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ jobs:
2626
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
2727
- uses: actions/checkout@v3
2828

29-
- name: Flake8
30-
run: flake8 .
29+
- name: Ruff
30+
run: |
31+
python3 -m pip install ruff
32+
ruff check .
33+
ruff format --check .
3134
3235
# Runs a single command using the runners shell
3336
- name: Run unit tests in serial

analytical_solution_generator.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55

66
def sympy_to_code(sympy_func):
7-
replacement_strings = (("pi", "np.pi"),
8-
("sin", "np.sin"),
9-
("cos", "np.cos"),
10-
("R.x", "x[0]"),
11-
("R.y", "x[1]"),
12-
("t", "self.t"),
13-
("*", " * "))
7+
replacement_strings = (
8+
("pi", "np.pi"),
9+
("sin", "np.sin"),
10+
("cos", "np.cos"),
11+
("R.x", "x[0]"),
12+
("R.y", "x[1]"),
13+
("t", "self.t"),
14+
("*", " * "),
15+
)
1416
code = str(sympy_func)
1517
for s in replacement_strings:
1618
code = code.replace(s[0], s[1])
@@ -43,6 +45,6 @@ def sympy_to_code(sympy_func):
4345
print(f"\ndT_dn_right = {kappa_dT_dn_right_code}")
4446

4547
# Solve for T_inf on top boundary
46-
T_inf_left = simplify(T + 1 / h * kappa_grad_T.dot(- R.i))
48+
T_inf_left = simplify(T + 1 / h * kappa_grad_T.dot(-R.i))
4749
T_inf_left_code = sympy_to_code(T_inf_left)
4850
print(f"\nT_inf_left = {T_inf_left_code}")

csut.py

+65-46
Original file line numberDiff line numberDiff line change
@@ -25,73 +25,92 @@
2525
material_mt = f.read_meshtags(mesh, "materials")
2626

2727
# TODO Give meaningful volume and boundary names
28-
volume_ids = {"volume_0": 0,
29-
"volume_1": 1,
30-
"volume_2": 2,
31-
"volume_3": 3,
32-
"volume_4": 4}
28+
volume_ids = {"volume_0": 0, "volume_1": 1, "volume_2": 2, "volume_3": 3, "volume_4": 4}
3329
boundary_ids = {}
34-
boundary_ids["T"] = {"boundary_0": 0,
35-
"boundary_1": 1,
36-
"boundary_2": 2,
37-
"boundary_3": 3}
38-
boundary_ids["u"] = {"boundary_0": 0,
39-
"boundary_1": 1,
40-
"boundary_2": 2}
30+
boundary_ids["T"] = {"boundary_0": 0, "boundary_1": 1, "boundary_2": 2, "boundary_3": 3}
31+
boundary_ids["u"] = {"boundary_0": 0, "boundary_1": 1, "boundary_2": 2}
4132

4233
# Add materials
43-
materials = {volume_ids["volume_0"]: mat_dict["Copper"],
44-
volume_ids["volume_1"]: mat_dict["CuCrZr"],
45-
volume_ids["volume_2"]: mat_dict["304SS"],
46-
volume_ids["volume_3"]: mat_dict["304SS"],
47-
volume_ids["volume_4"]: mat_dict["304SS"]}
34+
materials = {
35+
volume_ids["volume_0"]: mat_dict["Copper"],
36+
volume_ids["volume_1"]: mat_dict["CuCrZr"],
37+
volume_ids["volume_2"]: mat_dict["304SS"],
38+
volume_ids["volume_3"]: mat_dict["304SS"],
39+
volume_ids["volume_4"]: mat_dict["304SS"],
40+
}
4841

4942

5043
# Boundary conditions
5144
bcs = {}
52-
bcs["T"] = {boundary_ids["T"]["boundary_0"]:
53-
{"type": "convection",
54-
"value": lambda x: 293.15 * np.ones_like(x[0]),
55-
"h": lambda T: 5},
56-
boundary_ids["T"]["boundary_1"]:
57-
{"type": "heat_flux",
58-
"value": lambda x: 1.6e5 * np.ones_like(x[0])},
59-
boundary_ids["T"]["boundary_2"]:
60-
{"type": "heat_flux",
61-
"value": lambda x: 5e5 * np.ones_like(x[0])},
62-
boundary_ids["T"]["boundary_3"]:
63-
{"type": "convection",
64-
"value": lambda x: 293.15 * np.ones_like(x[0]),
65-
"h": mat_dict["water"]["h"]}}
66-
bcs["u"] = {boundary_ids["u"]["boundary_0"]:
67-
{"type": "displacement",
68-
"value": np.array([0, 0, 0], dtype=PETSc.ScalarType)},
69-
boundary_ids["u"]["boundary_1"]:
70-
{"type": "displacement",
71-
"value": np.array([0, 0, 0], dtype=PETSc.ScalarType)},
72-
boundary_ids["u"]["boundary_2"]:
73-
{"type": "pressure",
74-
"value": fem.Constant(mesh, PETSc.ScalarType(-1e3))}}
45+
bcs["T"] = {
46+
boundary_ids["T"]["boundary_0"]: {
47+
"type": "convection",
48+
"value": lambda x: 293.15 * np.ones_like(x[0]),
49+
"h": lambda T: 5,
50+
},
51+
boundary_ids["T"]["boundary_1"]: {
52+
"type": "heat_flux",
53+
"value": lambda x: 1.6e5 * np.ones_like(x[0]),
54+
},
55+
boundary_ids["T"]["boundary_2"]: {
56+
"type": "heat_flux",
57+
"value": lambda x: 5e5 * np.ones_like(x[0]),
58+
},
59+
boundary_ids["T"]["boundary_3"]: {
60+
"type": "convection",
61+
"value": lambda x: 293.15 * np.ones_like(x[0]),
62+
"h": mat_dict["water"]["h"],
63+
},
64+
}
65+
bcs["u"] = {
66+
boundary_ids["u"]["boundary_0"]: {
67+
"type": "displacement",
68+
"value": np.array([0, 0, 0], dtype=PETSc.ScalarType),
69+
},
70+
boundary_ids["u"]["boundary_1"]: {
71+
"type": "displacement",
72+
"value": np.array([0, 0, 0], dtype=PETSc.ScalarType),
73+
},
74+
boundary_ids["u"]["boundary_2"]: {
75+
"type": "pressure",
76+
"value": fem.Constant(mesh, PETSc.ScalarType(-1e3)),
77+
},
78+
}
7579

7680
# Elastic source term (not including gravity)
7781
f_u = fem.Constant(mesh, np.array([0, 0, 0], dtype=PETSc.ScalarType))
7882

7983

8084
# Thermal source term
81-
def f_T(x): return np.zeros_like(x[0])
85+
def f_T(x):
86+
return np.zeros_like(x[0])
8287

8388

8489
# Initial temperature
85-
def T_0(x): return 293.15 * np.ones_like(x[0])
90+
def T_0(x):
91+
return 293.15 * np.ones_like(x[0])
8692

8793

8894
# Acceleration due to gravity
89-
g = PETSc.ScalarType(- 9.81)
95+
g = PETSc.ScalarType(-9.81)
9096

9197
# Solve the problem
92-
results = solve(mesh, k, delta_t, num_time_steps, T_0, f_T,
93-
f_u, g, materials, material_mt, bcs, bc_mt,
94-
write_to_file=write_to_file, steps_per_write=steps_per_write)
98+
results = solve(
99+
mesh,
100+
k,
101+
delta_t,
102+
num_time_steps,
103+
T_0,
104+
f_T,
105+
f_u,
106+
g,
107+
materials,
108+
material_mt,
109+
bcs,
110+
bc_mt,
111+
write_to_file=write_to_file,
112+
steps_per_write=steps_per_write,
113+
)
95114

96115
# Write timing and iteration data to file
97116
n_procs = MPI.COMM_WORLD.Get_size()

gmsh_demo.py

+45-23
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
def create_mesh(comm, h):
1212
volume_ids = {"volume": 1}
13-
boundary_ids = {"front": 1,
14-
"back": 2,
15-
"sides": 3}
13+
boundary_ids = {"front": 1, "back": 2, "sides": 3}
1614

1715
gmsh.initialize()
1816
if comm.rank == 0:
@@ -40,7 +38,8 @@ def create_mesh(comm, h):
4038

4139
partitioner = mesh.create_cell_partitioner(mesh.GhostMode.none)
4240
msh, ct, ft = gmshio.model_to_mesh(
43-
gmsh.model, comm, 0, gdim=3, partitioner=partitioner)
41+
gmsh.model, comm, 0, gdim=3, partitioner=partitioner
42+
)
4443
gmsh.finalize()
4544

4645
return msh, ct, ft, volume_ids, boundary_ids
@@ -60,19 +59,27 @@ def create_mesh(comm, h):
6059

6160
# Specify boundary conditions
6261
bcs = {}
63-
bcs["T"] = {boundary_ids["back"]:
64-
{"type": "temperature",
65-
"value": lambda x: 293.15 * np.ones_like(x[0])},
66-
boundary_ids["sides"]:
67-
{"type": "convection",
68-
"value": lambda x: 293.15 * np.ones_like(x[0]),
69-
"h": lambda T: 5},
70-
boundary_ids["front"]:
71-
{"type": "heat_flux",
72-
"value": lambda x: 1e5 * np.ones_like(x[0])}}
73-
bcs["u"] = {boundary_ids["back"]:
74-
{"type": "displacement",
75-
"value": np.array([0, 0, 0], dtype=PETSc.ScalarType)}}
62+
bcs["T"] = {
63+
boundary_ids["back"]: {
64+
"type": "temperature",
65+
"value": lambda x: 293.15 * np.ones_like(x[0]),
66+
},
67+
boundary_ids["sides"]: {
68+
"type": "convection",
69+
"value": lambda x: 293.15 * np.ones_like(x[0]),
70+
"h": lambda T: 5,
71+
},
72+
boundary_ids["front"]: {
73+
"type": "heat_flux",
74+
"value": lambda x: 1e5 * np.ones_like(x[0]),
75+
},
76+
}
77+
bcs["u"] = {
78+
boundary_ids["back"]: {
79+
"type": "displacement",
80+
"value": np.array([0, 0, 0], dtype=PETSc.ScalarType),
81+
}
82+
}
7683

7784
bc_mt = {}
7885
bc_mt["T"] = ft
@@ -83,17 +90,32 @@ def create_mesh(comm, h):
8390

8491

8592
# Thermal source function
86-
def f_T(x): return np.zeros_like(x[0])
93+
def f_T(x):
94+
return np.zeros_like(x[0])
8795

8896

8997
# Initial temperature
90-
def T_0(x): return 293.15 * np.ones_like(x[0])
98+
def T_0(x):
99+
return 293.15 * np.ones_like(x[0])
91100

92101

93102
# Acceleration due to gravity
94-
g = PETSc.ScalarType(- 9.81)
103+
g = PETSc.ScalarType(-9.81)
95104

96105
# Solve the problem
97-
results = solve(msh, k, delta_t, num_time_steps, T_0, f_T,
98-
f_u, g, materials, ct, bcs, bc_mt,
99-
write_to_file=True, steps_per_write=1)
106+
results = solve(
107+
msh,
108+
k,
109+
delta_t,
110+
num_time_steps,
111+
T_0,
112+
f_T,
113+
f_u,
114+
g,
115+
materials,
116+
ct,
117+
bcs,
118+
bc_mt,
119+
write_to_file=True,
120+
steps_per_write=1,
121+
)

0 commit comments

Comments
 (0)